subset of the actions observable.subscribe(onNextAction, onErrorAction, onCompleteAction); observable.subscribe(onNextAction, onErrorAction); observable.subscribe(onNextAction); public interface Action1<T> { public void call(T value); }
public void call(Subscriber<? super String> subscriber) { // Do some more stuff subscriber.onNext("Hello, GDG Bucharest!"); subscriber.onCompleted(); } });
public void call(Subscriber<? super String> subscriber) { // Do some more stuff subscriber.onNext("Hello, GDG Bucharest!"); subscriber.onCompleted(); } }); Observable.create(subscriber -> { // Do some more stuff subscriber.onNext("Hello, GDG Bucharest!"); subscriber.onCompleted(); });
“stabilized” Steps: Observable + Debounce operator Watcher for text change events on InputTextView Conversion to observable events Debounce for limiting
results from cache Steps: Concat / Merge operator Create observables for both types of load Merge the results and handle in common way Cache prefetching - the idea
= loadFreshData(); Observable.concat(cacheDataLoader, freshDataLoader) .observeOn(AndroidSchedulers.mainThread()) .subscribe( data -> { // Update the UI with the available data mAdapter.setNewData(data); });
versatile API / library supporting both async or sync use Steps: Observable + subscribeOn()/observeOn() Expose Cold Observables (perform work upon subscription) Can be run either synchronously or asynchronously
to APIs Steps: Retrofit + Operators + subscribeOn()/observeOn() Use the Rx-aware Retrofit calls Run network calls on Schedulers.io() Chain processing Switch back to main thread for UI updates
posts list to individual items .flatMap(posts -> Observable.from(posts)) // Process each item .doOnNext(post -> storeToLocalDatabase(post)) // Take just the last post .last() // Configure threads properly .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) // Handle final results .subscribe(post -> { updateUI(post); }, (error) -> { Log.e(TAG, "Network error!", error); } );