ReactiveX on Android
http://reactivex.io/tutorials.html
Android
- The RxAndroid Project
- Introduction to RxJava for Android by Matt Dupree (and part 2)
- Getting Started With ReactiveX on Android by Ashraff Hathbelagal
- Asynchronous Android Programming (The Good, the Bad, and the Ugly) by Devin Corrigall
- Grokking RxJava, Part 4: Reactive Android — by Dan Lew
- RxJava on Android: PopupMenus and Dialogs by Adel Nizamutdinov
- Tackling Complexity in Android Apps with RxJava at SoundCloud: Matt McComb interviews Matthias Käppler
- Resources for Learning RxJava for Android by Greg Loesch
- Top 7 Tips for RxJava on Android
- RxBinding — easy one-way binding support for Android, based on RxJava
- G-Rex — a tiny library that assists in saving and restoring objects to and from disk using RxJava on Android
- Replace AsyncTask and AsyncTaskLoader with rx.Observable by Ross Hambrick
- How to Keep Your RxJava Subscribers from Leaking by Matt Dupree
- Android reactive programming with RxJava video of a presentation by Ivan Morgillo
- Learning RxJava (for Android) by example video and speaker deck of a presentation by Kaushik Gopal, and the associated GitHub repository
- Retrofit 2.0: The biggest update yet on the best HTTP Client Library for Android, which now supports RxJava Observables
- Fast Android Networking Fast Android Networking which is made on top of OkHttp Client Layer now supports RxJava
- RxJava2-Android-Samples Learning RxJava2.0 for Android by Amit Shekhar
https://www.philosophicalhacker.com/2015/06/19/introduction-to-rxjava-for-android-pt-2/
Using Fast Android Networking Library With RxJava(best)
https://github.com/amitshekhariitbhu/Fast-Android-Networking/wiki/Using-Fast-Android-Networking-Library-With-RxJava
RxJava 2 Android Examples – Migration From RxJava 1 to RxJava 2 – How to use RxJava 2 in Android
https://github.com/amitshekhariitbhu/RxJava2-Android-Samples
https://github.com/kaushikgopal/RxJava-Android-Samples#10-simple-timing-demos-using-timer-interval-and-delay
Learn RxJava
https://mindorks.com/course/learn-rxjava
The key lines here are the subscribeOn()
and observeOn()
lines. These lines take Schedulers
that determine the threads on which asynchronous data is created and consumed, respectively. We pass a Scheduler
to subscribeOn()
that schedules the asynchronous data to be created on a background io thread and we pass a Scheduler
to the observeOn()
method that ensures that the asynchronous data is consumed on the main thread.
One quick thing to note here is that the AndroidSchedulers.mainThread()
method is not actually a part of RxJava. Its a part of RxAndroid.
http://blog.danlew.net/2014/10/08/grokking-rxjava-part-4/
First, there’s AndroidSchedulers
which provides schedulers ready-made for Android’s threading system. Need to run some code on the UI thread? No problem – just use AndroidSchedulers.mainThread()
:
retrofitService.getImage(url)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bitmap -> myImageView.setImageBitmap(bitmap));
If you’ve got your own Handler
, you can create a scheduler linked to it with HandlerThreadScheduler
1.
Next we have AndroidObservable
which provides more facilities for working within the Android lifecycle. There is bindActivity()
and bindFragment()
which, in addition to automatically using AndroidSchedulers.mainThread()
for observing, will also stop emitting items when your Activity
or Fragment
is finishing (so you don’t accidentally try to change state after it is valid to do so).
AndroidObservable.bindActivity(this, retrofitService.getImage(url))
.subscribeOn(Schedulers.io())
.subscribe(bitmap -> myImageView.setImageBitmap(bitmap));
I also like AndroidObservable.fromBroadcast()
, which allows you to create an Observable
that works like a BroadcastReceiver
. Here’s a way to be notified whenever network connectivity changes:
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
AndroidObservable.fromBroadcast(context, filter)
.subscribe(intent -> handleConnectivityChange(intent));
Finally, there is ViewObservable
, which adds a couple bindings for Views
. There’s ViewObservable.clicks()
if you want to get an event each time a View
is clicked, or ViewObservable.text()
to observe whenever a TextView
changes its content.
ViewObservable.clicks(mCardNameEditText, false)
.subscribe(view -> handleClick(view));
Retrofit support for Observable
also makes it easy to combine multiple REST calls together. For example, suppose we have one call that gets the photo and a second that gets the metadata. We can zip the results together:
Observable.zip(
service.getUserPhoto(id),
service.getPhotoMetadata(id),
(photo, metadata) -> createPhotoWithData(photo, metadata))
.subscribe(photoWithData -> showPhoto(photoWithData));
Asynchronous Android Programming – the Good, the Bad, and the Ugly
// getImageNetworkCall returns Observable<Bitmap>
Subscription subscription = getImageNetworkCall()
.map(new Func1<Bitmap, BitmapDrawable>() {
@Override
public BitmapDrawable call(Bitmap bitmap) {
// Convert Bitmap to BitmapDrawable
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(imageObserver);
Another useful operator is merge, which uses multiple Observables, and emits items from all of the Observables to the same Observer, calling onNext once for every item emitted from each Observable. If an error is received from either Observable during this process, onError is called and no more items are emitted. If this isn’t desired, mergeDelayError can be used instead, which works the same as merge, but if there is an error, it waits until all Observables complete or have errors before triggering “onError”.
// getImage returns Observable<Bitmap>
// getAnotherImage returns Observable<Bitmap>
Subscription subscription = Observable.merge(getImage(), getAnotherImage())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(imageObserver);
// The imageObserver’s onNext will get called twice, with a bitmap each time, followed by
// onComplete letting you know both bitmaps are done
The only clean up you need to do when using Observables and Subscriptions is make sure to unsubscribe the Subscription in the onDestroy method of your activity. This is a simple way to prevent memory leaks, and makes sense, as it follows the Android lifecycle.
RxAndroid Github page
https://github.com/ReactiveX/RxAndroid
https://www.youtube.com/watch?v=RQl1i4-mUa0&list=PLshdtb5UWjSrxqaRtsevmF8aCGGR-rgK1
by PRABEESH R K
https://code.tutsplus.com/tutorials/getting-started-with-reactivex-on-android–cms-24387