9. Reactive on Android
âą evading callback hell
âą How to execute heavy tasks on
background threads?
âą And deliver results on the main (UI)
thread?
15. AsyncTask
âą deals with the main thread
âą error-prone
âą difficult error propagation
âą difficult to bound to activity/fragment
lifecycle
âą difficult composition
16. class MyFragment extends Fragment implements
LoaderManager.LoaderCallbacks<String> {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(42, null, this);
}
@Override public Loader<String> onCreateLoader(int id, Bundle args) {
return new AsyncTaskLoader<String>(getActivity()) {
@Override public String loadInBackground() {
return doHeavyTask();
}
};
}
@Override public void onLoadFinished(Loader<String> loader, String data) {
showResult(data);
}
@Override public void onLoaderReset(Loader<String> loader) {}
}
Loaders
21. RxJava
âą Java VM implementation of Reactive
Extensions
âą a library for composing flows and
sequences of asynchronous data
âą open-source
âą https://github.com/ReactiveX/RxJava
âą by Netflix
22. RxJava
âą DSL for creating computation flows from
async sources
âą flows called Observables
âą push semantics of Observables
24. Future
proxy or wrapper around an object that is not
yet there
future.get()
future.isDone()
âą non-trivial complexity when nested
âą difficult to compose conditional
asynchronous execution flows
26. Iterable vs. Observable
// Iterable<String>
getData()
.skip(10)
.take(5)
.map({s ->
return s + â1â})
.forEach({
println it
});
// Observable<String>
getData()
.skip(10)
.take(5)
.map({s ->
return s + â1â})
.subscribe({
println it
});
27. Iterable vs. Observable
single items multiple items
synchronous T getData() Iterable<T> getData()
asynchronous Future<T> getData() Observable<T> getData()
28. Iterable vs. Observable
event Iterable (pull) Observable (push)
retrieve data T next() onNext(T)
discover error throws Exception onError(Exception)
complete !hasNext() onCompleted()
29. Observable
Calling Thread
public Observable<String> getData();
Callback Thread
synchronous on calling thread
30. Observable
Calling Thread
public Observable<String> getData();
Callback Thread
Thread Pool
asynchronous on a separate thread
31. Observable
Calling Thread
public Observable<String> getData();
Callback Threads
Thread Pool
asynchronous on multiple threads
32. Observable
âą not biased toward some particular source
of asynchronicity
âą the implementation chooses if the code will
be blocking or non-blocking
42. RxAndroid
Android specific things for Rx
âą Scheduler
âą reliable and thread-safe with regarding
Fragment/Activity life-cycle
âą reactive components for Android use cases
and UI
55. Creating Observables
âą convert existing data structure into an
Observable
from()
â Iterable, Future, Array
repeat()
â emit source Observable repeatedly
56. Creating Observables
âą convert existing data structure into an
Observable
timer()
â emits a single item after a given time
57. Creating Observables
âą convert existing data structure into an
Observable
empty()
â emits nothing and completes
error()
â emits nothing and signals an error
never()
â emits nothing at all