rxjava - yow! conferences · what is reactive programming? reactive programming is a programming...
TRANSCRIPT
![Page 1: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/1.jpg)
RxJavaand WHY you should care
Jeroen Tietema
![Page 2: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/2.jpg)
Overviewintro ofRxJavawhy?some tips
![Page 3: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/3.jpg)
What is RxJava?Java implementation of Reactive ExtensionsReactive Extensions is .Net implementation of ReactiveProgramming
![Page 4: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/4.jpg)
What is Reactive programming?Reactive programming is a programming paradigmoriented around data ᴀ밄ows and the propagation of
change.
This means that it should be possible to express staticor dynamic data ᴀ밄ows with ease in the programming
languages used, and that the underlying executionmodel will automatically propagate changes through
the data ᴀ밄ow. – Wikipedia (Reactive Programming)
TLDR; something with data ᴀ밄ows?
![Page 5: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/5.jpg)
What is Reactive Extensions?Using Rx, developers represent asynchronous data
streams with Observables, query asynchronous datastreams using LINQ operators, and parameterize theconcurrency in the asynchronous data streams using
Schedulers.
Simply put, Rx = Observables + LINQ + Schedulers. –Microsoft Reactice Extensions Homepage
![Page 6: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/6.jpg)
RxJava conceptsObservables and the ObservableContractSubscribing and subscriptionsoperators
![Page 7: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/7.jpg)
ObservablesObservables emit data when you subscribe to them.
onNext (0..∞)onError (0..1)onCompleted(0..1)
Cannot call both onError and onCompleted.
Should never do any calls after the Subscriber unsubscribes.
When multiple observers subscribe to the same Observable, it is up tothe Observable to decide what to do with even emission.
![Page 8: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/8.jpg)
Subscribers and SubscriptionsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
![Page 9: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/9.jpg)
Subscribers and SubscriptionsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
Subscription s = myInts.subscribe(new Subscriber<Integer>() { void onNext(Integer i) { // your Integer has arrived } void onCompleted() { // Happy Friday!!! } void onError(Throwable e) { // :'‐( }});
![Page 10: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/10.jpg)
Subscribers and SubscriptionsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
Subscription s = myInts.subscribe(new Subscriber<Integer>() { void onNext(Integer i) { // your Integer has arrived } void onCompleted() { // Happy Friday!!! } void onError(Throwable e) { // :'‐( }});// boring, boring!s.unsubscribe();
![Page 11: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/11.jpg)
OperatorsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
![Page 12: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/12.jpg)
OperatorsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
Observable<Integer> evenNumbers = myInts.filter(i ‐> i % 2 == 0)
![Page 13: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/13.jpg)
OperatorsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);
Observable<Integer> evenNumbers = myInts.filter(i ‐> i % 2 == 0);
Observable<String> myIntsAsStrings = evenNumbers .map(i ‐> "My lovely int " + i);
![Page 14: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/14.jpg)
OperatorsObservable<Integer> myInts = Observable.just(1, 2, 3, 4, 5);Observable<Integer> evenNumbers = myInts.filter(i ‐> i % 2 == 0);
Observable<String> myIntsAsStrings = evenNumbers .map(i ‐> "My lovely int " + i);// Subscribing results in:onNext("My lovely int 2");onNext("My lovely int 4");onCompleted();
![Page 15: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/15.jpg)
Operators (continued)Observable<Integer> helloAsyncInts = Observable.just(1, 2, 3, 4); // start emitting on Schedulers.io() .subscribeOn(Schedulers.io()) // emit the results on main UI thread .observeOn(AndroidSchedulers.mainThread());
![Page 16: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/16.jpg)
Operators (continued)Observable<Integer> helloAsyncInts = Observable.just(1, 2, 3, 4); // start emitting on Schedulers.io() .subscribeOn(Schedulers.io()) // emit the results on main UI thread .observeOn(AndroidSchedulers.mainThread());
You can call observeOn multiple times!AndroidSchedulers comes fromRxAndroid
![Page 17: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/17.jpg)
Operators (continued)Observable<Integer> helloAsyncInts = Observable.just(1, 2, 3, 4); // add a delay to emitted items on // Schedulers.computation() .delay(500, TimeUnit.MILLISECONDS)
![Page 19: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/19.jpg)
Retrofit vs RxJava (async)myRetrofitApi.someCall( new Callback<SomePojo>() { void onSuccess(SomePojo myPojo) { // your awesomeness }
void onError(RetrofitError error) { // deal with it } });
Subscription s = myObservableApi.someCall() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<SomePojo>() { void onNext(SomePojo myPojo) { // your awesomeness } void onError(Throwable error) { // deal with it }
void onCompleted() { // but ... why? } });
![Page 20: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/20.jpg)
Retrofit vs RxJava (sync)SomePojo myPojo = myRetrofitApi.someCall(); Subscription s = myObservableApi.someCall()
.subscribe(new Subscriber<SomePojo>() { void onNext(SomePojo myPojo) { // your awesomeness } void onError(Throwable error) { // deal with it }
void onCompleted() { // but ... why? } });
![Page 22: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/22.jpg)
Threading dilemma (or not?)// syncSomePojo myPojo = myRetrofitApi.someCall();
Observable<SomePojo> myObservableApi.someCall();
// asyncmyRetrofitApi.someCall(myCallback);
Observable<SomePojo> myObservableApi.someCall();
Decisions around threading are big decisions, but we end up makingthem in a ad-hoc fashion
RxJava: You can worry about threading later!
![Page 23: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/23.jpg)
For example
![Page 24: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/24.jpg)
Observable<Account> observable = myDatabase.findAccount();
![Page 25: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/25.jpg)
Observable<HomePageFeed> observable = myDatabase.findAccount() .flatMap(myAccount ‐> myApi.fetchHomePageFeed(myAccount))
![Page 26: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/26.jpg)
Observable<HomePageFeed> observable = myDatabase.findAccount() .flatMap(myAccount ‐> myApi.fetchHomePageFeed(myAccount)) .doOnNext(myHomePageFeed ‐> myCache.store(myHomePageFeed))
![Page 27: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/27.jpg)
Observable<HomePageFeed> observable = myDatabase.findAccount() .flatMap(myAccount ‐> myApi.fetchHomePageFeed(myAccount)) .doOnNext(myHomePageFeed ‐> myCache.store(myHomePageFeed)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread());
![Page 28: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/28.jpg)
Subscription s = myDatabase.findAccount() .flatMap(myAccount ‐> myApi.fetchHomePageFeed(myAccount)) .doOnNext(myHomePageFeed ‐> myCache.store(myHomePageFeed)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<HomePageFeed>() { void onNext(HomePageFeed myHomePageFeed) { // render homepage feed } void onError(Throwable error) { // show error dialog } void onCompleted(){} });
![Page 29: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/29.jpg)
Compared to:
![Page 30: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/30.jpg)
myDatabase.findAccount(new DatabaseCallback<Account>() {
void onQueryComplete(Account account){ }});
![Page 31: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/31.jpg)
myDatabase.findAccount(new DatabaseCallback<Account>() {
void onQueryComplete(Account account){ myApi.getHomePage(account, new ApiCallback<HomePageFeed>() { void onResponse(HomePageFeed homepage) { } }); }});
![Page 32: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/32.jpg)
myDatabase.findAccount(new DatabaseCallback<Account>() {
void onQueryComplete(Account account){ myApi.getHomePage(account, new ApiCallback<HomePageFeed>() { void onResponse(HomePageFeed homepage) { myCache.store(homepage); } }); }});
![Page 33: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/33.jpg)
myDatabase.findAccount(new DatabaseCallback<Account>() {
void onQueryComplete(Account account){ myApi.getHomePage(account, new ApiCallback<HomePageFeed>() { void onResponse(HomePageFeed homepage) { myCache.store(homepage); // <‐‐ Disk IO on UI thread! } }); }});
![Page 34: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/34.jpg)
myDatabase.findAccount(new DatabaseCallback<Account>() {
void onQueryComplete(Account account){ myApi.getHomePage(account, new ApiCallback<HomePageFeed>() { void onResponse(HomePageFeed homepage) { new AsyncTask<Void, Void, Void>() { Void doInBackground(Void... voids) { myCache.store(homepage); return null; } }.execute(); } }); }});
![Page 35: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/35.jpg)
Benefitswriting complicated threaded code becomes (relatively) easy.clear pattern to cancel background tasks throughSubscriptions
![Page 36: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/36.jpg)
![Page 37: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/37.jpg)
![Page 38: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/38.jpg)
![Page 39: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/39.jpg)
![Page 40: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/40.jpg)
© rjp
Operators, operators, operators!!manipulating datacan do threadingbut also replace conditionallogic
![Page 41: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/41.jpg)
For exampleSubscription s = myCache.fetchHomePageFeed() .concat(myApi.fetchHomePageFeed()) .first() .subscribe(new Subscriber // etc.
![Page 42: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/42.jpg)
For exampleSubscription s = myCache.fetchHomePageFeed() .concat(myApi.fetchHomePageFeed()) .first() .subscribe(new Subscriber // etc.
vsHomePageFeed myFeed = myCache.fetchHomePageFeed();if (myFeed == null) { myFeed = myApi.fetchHomePageFeed();}return myFeed;
![Page 43: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/43.jpg)
Search exampleclass MySearchView extends TextView {
PublishSubject<String> subject = PublishSubject.create();
void init() { addTextChangedListener(new TextWatcher() { // .. void afterTextChanged(Editable s) { subject.onNext(s.toString()); } }); }
Observable<String> getTextObservable() { return subject; }}
![Page 44: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/44.jpg)
Search examplemySearchView.getTextObservable() .filter(text ‐> text.length > 2)
![Page 45: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/45.jpg)
Search examplemySearchView.getTextObservable() .filter(text ‐> text.length > 2) .debounce(400, TimeUnit.MILLISECONDS)
![Page 46: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/46.jpg)
Search examplemySearchView.getTextObservable() .filter(text ‐> text.length > 2) .debounce(400, TimeUnit.MILLISECONDS) .observeOn(Schedulers.io()) .flatMap(text ‐> myApi.search(text))
![Page 47: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/47.jpg)
Search examplemySearchView.getTextObservable() .filter(text ‐> text.length > 2) .debounce(400, TimeUnit.MILLISECONDS) .observeOn(Schedulers.io()) .flatMap(text ‐> myApi.search(text)) .observeOn(AndroidSchedulers.mainThread())
![Page 48: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/48.jpg)
Search exampleSubscription s = mySearchView.getTextObservable() .filter(text ‐> text.length > 2) .debounce(400, TimeUnit.MILLISECONDS) .observeOn(Schedulers.io()) .flatMap(text ‐> myApi.search(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { void onNext(SearchResults results) { // render results } void onError(Throwable someError) {} void onCompleted() {} });
![Page 49: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/49.jpg)
Search vanilla examplemyTextView.addTextChangedListener(new TextWatcher() {
void afterTextChanged(final Editable s) { if (s.toString().length > 2) {
} }});
![Page 50: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/50.jpg)
Search vanilla examplemyTextView.addTextChangedListener(new TextWatcher() {
void afterTextChanged(final Editable s) { if (s.toString().length > 2) { myHandler.postDelayed(new Runnable() { @Override void run() {
} }, 400L); } }});
![Page 51: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/51.jpg)
Search vanilla examplemyTextView.addTextChangedListener(new TextWatcher() {
void afterTextChanged(final Editable s) { if (s.toString().length > 2) { myHandler.postDelayed(new Runnable() { @Override void run() { myAsyncTask = new FetchSuggestionsTask(); myAsyncTask.execute(s.toString()); } }, 400L); } }});
![Page 52: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/52.jpg)
Search vanilla examplemyTextView.addTextChangedListener(new TextWatcher() {
void afterTextChanged(final Editable s) { if (s.toString().length > 2) { if (myCallback != null) { myHandler.removeCallbacks(myCallback); } myCallback = new Runnable() { @Override void run() { myAsyncTask = new FetchSuggestionsTask(); myAsyncTask.execute(s.toString()); } } myHandler.postDelayed(myCallback, 400L); } }});
![Page 53: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/53.jpg)
Search exampleSubscription s = mySearchView.getTextObservable() .filter(text ‐> text.length > 2) .debounce(400, TimeUnit.MILLISECONDS) .observeOn(Schedulers.io()) .flatMap(text ‐> myApi.search(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { void onNext(SearchResults results) { // render results } void onError(Throwable someError) {} void onCompleted() {} });
![Page 55: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/55.jpg)
Creating Observablesclass MyObservableApi {
public Observable<HomePageFeed> getHomePage(Account account) { // wrong return Observable.just( mySyncLegacyApi.getHomePage(account)); }}
![Page 56: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/56.jpg)
Defer + just = your friend!class MyObservableApi {
public Observable<HomePageFeed> getHomePage(Account account) { // right! return Observable.defer(() ‐> Observable.just( mySyncLegacyApi.getHomePage(account)); }}
![Page 57: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/57.jpg)
Testing model layer@Testpublic void getHomePage_givenValidAccount_returnsHomePage() { TestSubscriber<HomePageFeed> s = new TestSubscriber<>(); myObservableApi.subscribe(s); s.assertCompleted(); s.assertValueCount(1); HomePageFeed h = s.getOnNextValues().get(0);}
![Page 58: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/58.jpg)
Testing Presenter layer (Rx HQ)Inject your Schedulers
Schedulers.immediate() will su鍉䨈ce most of the time.
Use TestScheduler for more advanced use cases.
![Page 59: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/59.jpg)
Testing time based logic@Testpublic void test400MilliesDelay() { TestScheduler testScheduler = new TestScheduler(); TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
Observable.just(1) .delay(400, TimeUnit.MILLISECONDS, testScheduler) .subscribe(testSubscriber);
testScheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS); testSubscriber.assertNoValues();
testScheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS); testSubscriber.assertValueCount(1); testSubscriber.assertCompleted();}
![Page 60: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/60.jpg)
Useful linksFunctional Reactive Programming with RxJava, Ben Christensen,
Netᴀ밄ixhttps://www.youtube.com/watch?v=_t06LRX0DV0
Going Reactive, An Android Architectural Journey Matthias Käppler,Soundcloud
https://www.youtube.com/watch?v=R16OHcZJTno
Grokking RxJava, Dan Lew, Trellohttp://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/http://blog.danlew.net/2014/09/22/grokking-rxjava-part-2/http://blog.danlew.net/2014/09/30/grokking-rxjava-part-3/http://blog.danlew.net/2014/10/08/grokking-rxjava-part-4/
![Page 62: RxJava - YOW! Conferences · What is Reactive programming? Reactive programming is a programming paradigm oriented around data ᴀ밄ows and the propagation of change. This means](https://reader036.vdocuments.mx/reader036/viewer/2022062317/5ec67c8eae6d26098433808a/html5/thumbnails/62.jpg)
Thank youSlides are at:
https://jeroen.tietema.net/presentations/rxjava-yow/
Contact me at:@jtietema / [email protected]