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
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