Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Observable vs. LiveData
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Seiya Kokushi
February 27, 2018
Technology
1.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Observable vs. LiveData
Seiya Kokushi
February 27, 2018
More Decks by Seiya Kokushi
See All by Seiya Kokushi
OS 標準のデザインシステムを超えて - より柔軟な Flutter テーマ管理 | FlutterKaigi 2024
ronnnnn
0
1.8k
Kotlin/NativeのNew Memory Managerに移行しよう | Kotlin Fest 2022
ronnnnn
0
830
Kotlin Multiplatform MobileのおさらいとABEMAでのマルチプラットフォーム対応 | CA BASE NEXT 2022
ronnnnn
0
250
Compose Multiplatformってどうなんだろう? | Flutter × Kotlin Multiplatform by CyberAgent #7
ronnnnn
0
1k
Multiplatform Engineering Roadmap for the Future | ABEMA Developer Conference 2021
ronnnnn
0
130
エンジニアとしてのプロダクト貢献
ronnnnn
2
100k
事業を伸ばすためにエンジニアとして何ができるか
ronnnnn
3
2.2k
Other Decks in Technology
See All in Technology
20260619 私の日常業務での生成 AI 活用
masaruogura
1
230
Agent Skills設計で柔軟性と硬さのバランスが難しい話
nassy20
0
140
Oracle Cloud Infrastructure:2026年6月度サービス・アップデート
oracle4engineer
PRO
0
130
手塩にかけりゃいいってもんじゃない
ming_ayami
0
610
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
130
When Platform Engineering Meets GenAI
sucitw
0
130
ロボティクスの技術 / Robotics Technology
ks91
PRO
0
110
AIのReact習熟度を測る
uhyo
2
650
ACE-Step-1.5で見る 音楽生成AIのしくみと“破綻だけ直す”Retake機能の開発【zennfes spring 2026 登壇資料】
personabb
1
540
2026TECHFRESH畢業分享會 - Lightning Talk - 資料也要 CI/CD? 用 Airbyte 自動化資料同步
line_developers_tw
PRO
0
1.3k
生成 AI 実践ガイド (概略版) AIガバナンス編
asei
0
120
新しいUbuntu/GNOMEが使いたいからXからWaylandへ移行頑張ってるの巻 2026-06-20
nobutomurata
0
150
Featured
See All Featured
The Cult of Friendly URLs
andyhume
79
6.9k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
200
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
310
Everyday Curiosity
cassininazir
0
230
KATA
mclloyd
PRO
35
15k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Music & Morning Musume
bryan
47
7.2k
Transcript
Observable vs. LiveData CA.apk #5 Seiya Kokushi
Seiya Kokushi ronnnnn ronnnnn_jp
Motivation
ある日のMTGにて.. それじゃあ、ViewとViewModelは Observableでやりとりしましょ! さんせ〜い!! よーし、新しいアーキテクチャは MVVMを採用するぞ〜!
な、なぜLiveDataじゃないんだ!! そもそも、ObservableとLiveDataの 違いって何だ〜!!!
Overview
✂ ✂ Observable LiveData - android.databinding - interface - BaseObservable
ObservableField<T> ObservableXxx ObservableListなど - bindable - android.arch.lifecycle - abstract class - MutableLiveData<T>など - lifecycle aware
Pros and cons
View ViewModel Observable LiveData Data action fetch flowable observe update
Observable class MainViewModel @Inject constructor( private val randomUseCase: RandomUseCase )
: ViewModel() { val title: ObservableField<String> = ObservableField("") val imageUrl: ObservableField<String> = ObservableField("") … init { randomUseCase.observe() .subscribeOn(Schedulers.io()) .subscribe({ randomData -> title.set(randomData.data.title) imageUrl.set(randomData.data.images.downsizedMedium.url) }) .let { compositeDisposable.add(it) } fetchRandomData() } … } 監視したいオブジェクトを Observableで宣言 値が流れてきたら 値をObservableにセット
<layout > <data> <variable name="viewModel" type="com.ronnnnn.androidsamples.ui.MainViewModel" /> </data> <RelativeLayout >
<Button android:onClick="@{() -> viewModel.updateRandom()}” /> <TextView android:text="@{viewModel.title}" /> <ImageView imageUrl="@{viewModel.imageUrl}" /> </RelativeLayout> </layout> 値に変更があったら viewのpropertyが変更される Observable
Observable - DataBindingを最大限活用できる - オブジェクトに変更があった時のみ、値を通知する public class ObservableField<T> extends BaseObservable
implements Serializable { … private T mValue; … /** * Set the stored value. */ public void set(T value) { if (value != mValue) { mValue = value; notifyChange(); } } }
Observable - Lifecycle Awareじゃない - メモリリーク..? - 値変更のlistenerが
WeakReferenceを継承 - 画面遷移には不向き - 最新の値をキャッシュする ので、戻るとまた遷移する private static class WeakListener<T> extends WeakReference<ViewDataBinding> { private final ObservableReference<T> mObservable; protected final int mLocalFieldId; private T mTarget; … public boolean unregister() { boolean unregistered = false; if (mTarget != null) { mObservable.removeListener(mTarget); unregistered = true; } mTarget = null; return unregistered; } … protected ViewDataBinding getBinder() { ViewDataBinding binder = get(); if (binder == null) { unregister(); // The binder is dead } return binder; } }
LiveData class MainViewModel @Inject constructor( private val randomUseCase: RandomUseCase )
: ViewModel() { val title: MutableLiveData<String> = MutableLiveData() val imageUrl: MutableLiveData<String> = MutableLiveData() … init { randomUseCase.observe() .subscribeOn(Schedulers.io()) .subscribe({ randomData -> title.postValue(randomData.data.title) imageUrl.postValue(randomData.data.images.downsizedMedium.url) }) .let { compositeDisposable.add(it) } fetchRandomData() } … } 監視したいオブジェクトを LiveDataで宣言 値が流れてきたら 値をLiveDataにpost
class MainActivity : AppCompatActivity() { … override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) component.inject(this) val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main).apply { viewModel = mainViewModel } mainViewModel.imageUrl.observe(this, Observer { it ?: return@Observer binding.gifImageView.load(it) }) mainViewModel.title.observe(this, Observer { it ?: return@Observer binding.titleTextView.text = it }) } … } 値が通知されたら viewのpropertyを更新する LiveData
- DataBindingを最大限活用できない - オブジェクトに変更がなくても、値を通知する LiveData protected void postValue(T value) {
boolean postTask; synchronized (mDataLock) { postTask = mPendingData == NOT_SET; mPendingData = value; } if (!postTask) { return; } ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable); } @MainThread protected void setValue(T value) { assertMainThread("setValue"); mVersion++; mData = value; dispatchingValue(null); }
LiveData - Lifecycle Aware - 厳密な画面遷移には不向き - LiveDataはonPauseの後に購読解除される -
onSaveInstanceState後の画面遷移を再現できない googlesamples/android-architecture-components/issues/63
Plus one
None
None
None
sample project ronnnnn/AndroidSamples observable livedata livedatabinding (AS 3.1 canary 6+)