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
Subscribe RxJava vs LiveData English
Search
Bruno Aybar
October 20, 2018
Programming
0
130
Subscribe RxJava vs LiveData English
2018 edition, in English
Bruno Aybar
October 20, 2018
Tweet
Share
More Decks by Bruno Aybar
See All by Bruno Aybar
Compose: Estados y Recomposición
bruno125
0
29
Annotation Processors vs Kotlin Plugins
bruno125
0
50
Understanding Kotlin Type System
bruno125
1
52
Subscribe { RxJava vs LiveData }
bruno125
1
280
Yo también quiero usar Kotlin
bruno125
0
280
Kotlin en Fandango Latam
bruno125
0
150
Android Custom Views
bruno125
0
200
Developing a Fan Made Version Of My University's App
bruno125
0
100
Other Decks in Programming
See All in Programming
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
103 Early Hints
sugi_0000
1
230
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
790
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
280
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
140
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
480
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
340
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
Jakarta EE meets AI
ivargrimstad
0
260
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
520
39k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
It's Worth the Effort
3n
183
28k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Done Done
chrislema
181
16k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Scaling GitHub
holman
458
140k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Music & Morning Musume
bryan
46
6.2k
For a Future-Friendly Web
brad_frost
175
9.4k
Transcript
Subscribe { RxJava vs LiveData }
@brunoaybarg @bruno.aybar Bruno125 Bruno Aybar Android Dev Peru Organizer Android
Engineer
• Allow us to observe changes over some kind of
data, and to react to those changes. • Propose a reactive paradigm • Do they do the same? RxJava & LiveData
OBSERVER pattern
OBSERVER pattern Design pattern that defines a dependency between objects
(…), in which one of them changes its state, it notifies about it to all of its dependants
OBSERVER RxJava LiveData pattern
RxJava
RxJava An API for asynchronous programming with observable streams Is
a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming ReactiveX RxJava RxSwift RxJS Rx.NET RxPY RxPHP …
Stream of Data states events data data data
Fuente: Imagen tomada de https://www.pinterest.com/pin/749708669188692420/
Subscriber<T>
Observable<T>
Flowable<T> RxJava 2:
package org.reactivestreams; public interface Subscriber<T> { public void onNext(T
t); public void onError(Throwable t); public void onComplete(); }
Flowable.just(1)
Flowable.just(1) Flowable.fromArray(1,2,3)
Flowable.create<Int>({ s -> }, BackpressureStrategy.BUFFER) Flowable.fromArray(1,2,3) Flowable.just(1)
Flowable.create<Int>({ s -> s.onNext(1) }, BackpressureStrategy.BUFFER) Flowable.fromArray(1,2,3)
Flowable.just(1)
Flowable.create<Int>({ s -> s.onNext(1) s.onNext(2) }, BackpressureStrategy.BUFFER) Flowable.just(1)
Flowable.fromArray(1,2,3)
Flowable.create<Int>({ s -> s.onNext(1) s.onNext(2) s.onNext(3) }, BackpressureStrategy.BUFFER) Flowable.just(1)
Flowable.fromArray(1,2,3)
Flowable.create<Int>({ s -> s.onNext(1) s.onNext(2) s.onNext(3) s.onComplete() }, BackpressureStrategy.BUFFER)
Flowable.just(1) The stream ends here! Flowable.fromArray(1,2,3)
Flowable.create<Int>({ s -> s.onNext(1) s.onNext(2) s.onNext(3) s.onComplete() s.onNext(4) }, BackpressureStrategy.BUFFER)
Flowable.just(1) Not emitted Flowable.fromArray(1,2,3)
Flowable.create<Int>({ s -> s.onNext(1) s.onNext(2) s.onNext(3) s.onError(Throwable(“Error!")) s.onNext(4) }, BackpressureStrategy.BUFFER)
Flowable.just(1) Not emitted Flowable.fromArray(1,2,3)
Flowable.fromArray(1,2,3) Flowable.create<Int>({ s -> // heavy transaction... // async operation...
}, BackpressureStrategy.BUFFER) Flowable.just(1)
Event subscription
Flowable.just(1).subscribe( { value -> log("onNext: $value") }, { error ->
log("onError: $error")}, { log("onComplete!") } )
Flowable.just(1).subscribe( { value -> log("onNext: $value") }, { error ->
log("onError: $error")}, { log("onComplete!") } )
val subscription = Flowable.just(1).subscribe( { value -> log("onNext: $value") },
{ error -> log("onError: $error")}, { log("onComplete!") } ) subscription.dispose()
Handling Threads
Controller UI DataSource
Controller Presenter ViewModel DataSource UI
Controller DataSource UI
Controller DataSource UI Background
Controller DataSource UI UI Thread
Flowable.just(1).subscribe( { value -> log("onNext: $value") }, { error ->
log("onError: $error")}, { log("onComplete!") } )
Flowable.just(1) .subscribeOn(Schedulers.computation()) .subscribe( { value -> log("onNext: $value") }, {
error -> log("onError: $error")}, { log("onComplete!") } ) Thread in which the operation will be performed
Flowable.just(1) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { value -> log("onNext: $value") },
{ error -> log("onError: $error")}, { log("onComplete!") } ) Thread in which we will listen to the stream emissions
Operations
Flowable.fromArray(1,2,3,4) .filter { it % 2 == 0 }
Flowable.fromArray(1,2,3,4) .filter { it % 2 == 0 } .map
{ it + 10 } Flowable.just(User(“1”, “Bruno”)) .map { it.name }
Combine CombineLatest And/Then/When Zip Switch Join ... Filtering Filter Distinct
First Last Take ... Transform Map FlatMap Scan GroupBy Buffer ... More: http:/ /reactivex.io/documentation/ operators.html
Steep learning curve
LiveData
LiveData Room Lifecycle ViewModel ARCHITECTURE COMPONENTS
LiveData Lifecycle +
Flowable.fromArray(1,2,3)
Flowable.fromArray(1,2,3) 1 2 3 LiveData<Int>
Flowable.fromArray(1,2,3) 1 2 3 MutableLiveData<Integer> data = new MutableLiveData<>();
data.setValue(1); //UI thread data.setValue(2); //UI thread data.postValue(3); //Background thread
Flowable.fromArray(1,2,3) 1 2 3 MutableLiveData<Integer> data = new MutableLiveData<>();
data.setValue(1); //UI thread data.setValue(2); //UI thread data.postValue(3); //Background thread
Event subscription
data.observe(lifecycleOwner, new Observer<Integer>() { @Override public void onChanged(@Nullable Integer value)
{ log("onChanged: $value”) } });
data.observe(lifecycleOwner, Observer<Int> { value -> log("onChanged: $value”) })
data.observe(lifecycleOwner, Observer<Int> { value -> log("onChanged: $value”) })
data.observe(lifecycleOwner, Observer<Int> { value -> log("onChanged: $value”) })
None
LifecycleOwner class that contains information about a component lifecycle, and
allow other objects to observe its changes
LifecycleOwner class that contains information about a component lifecycle, and
allow other objects to observe its changes Destroyed Created Started Resumed Initialized
None
LifecycleObserver allows you to observe the current state of a
LifecycleOwner
LifecycleObserver public class MyObserver implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public
void connectListener() { } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void disconnectListener() { } }
Flowable <Int> Activity 1 2 3 subscribes to data.subscribe {
value -> textView.setText("Text " + value) }
Flowable <Int> Activity subscribes to data.subscribe { value -> textView.setText("Text
" + value) } destroyed 4 5 6
LiveData <Int> Activity subscribes to data.observe(lifecycleOwner, { value -> textView.setText("Text
" + value) }) destroyed X
LiveData <Int> Activity subscribes to data.observe(lifecycleOwner, { value -> textView.setText("Text
" + value) }) STARTED | RESUMED
LiveData <Int> Activity subscribes to data.observe(lifecycleOwner, { value -> textView.setText("Text
" + value) }) STARTED | RESUMED 6
Handling threads
MutableLiveData<Integer> data = new MutableLiveData<>(); data.setValue(1); //UI thread data.postValue(2);
//Background thread
MutableLiveData<Integer> data = new MutableLiveData<>(); data.setValue(1); //UI thread data.postValue(2);
//Background thread data.observe(lifecycleOwner, { value -> // executes in UI thread })
Operations
val userLiveData: LiveData<User> = … val userNameLiveData = Transformations.map(
userLiveData, { it.name }) Map SwitchMap Custom Transformations
+ LiveData Lifecycle + ViewModel + Room
RxJava vs LiveData Explicit code ✔ Thread handling ✔ Powerful
Operators ✔ Steep learning curve ✘ Really simple ✔ Android-oriented ✔ Integration with AC ✔ Not so powerful operators ✘ Library compatibility (i.e. Retrofit) ✔ Over-engineered? ✘ Portable knowledge ✔
¯\_(ツ)_/¯ RxJava vs LiveData
Controller Presenter ViewModel Data Sources UI
Controller Presenter ViewModel Data Sources UI LiveData
Controller Presenter ViewModel Data Sources UI RxJava
¯\_(ツ)_/¯ RxJava vs LiveData
Additional Material Intro to RxJava (Christina Lee) https:/ /www.youtube.com/watch?v=XLH2v9deew0 Learning
Rx (for Android) by Example https:/ /www.youtube.com/watch?v=k3D0cWyNno4 Common RxJava Mistakes https:/ /www.youtube.com/watch?v=QdmkXL7XikQ RxJava in Baby Steps https:/ /www.youtube.com/watch?v=YPf6AYDaYf8 RxMarbles http:/ /rxmarbles.com/
Live Data docs https:/ /developer.android.com/topic/libraries/architecture/livedata.html LiveData & Lifecycle https:/ /www.youtube.com/watch?v=jCw5ib0r9wg
ViewModels, LiveData and Lifecycles, oh my! https:/ /www.youtube.com/watch?v=SlZVYkhoSq8 Android lifecycle-aware components codelab https:/ /codelabs.developers.google.com/codelabs/android-lifecycles Additional Material
@brunoaybarg @bruno.aybar Bruno125 Bruno Aybar Android Dev Perú Organizer Android
Engineer @ Avantica Gracias! https://speakerdeck.com/bruno125/subscribe-rxjava-vs-livedata-2018