ReactiveX on Android

print

ReactiveX on Android

 

http://reactivex.io/tutorials.html

Android

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

This course describes all aspects of RxJava required for Android developement.

https://mindorks.com/course/learn-rxjava

 

schedulers

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

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.

Code

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

Getting Started With ReactiveX on Android

by 
Difficulty:IntermediateLength:MediumLanguages:

Developing a complex Android app that has lots of network connections, user interactions, and animations often means writing code that is full of nested callbacks. Such code, sometimes called callback hell, is not only lengthy and hard to understand, but also error-prone. ReactiveX offers an alternative approach that is both clear and concise, to manage asynchronous tasks and events.

RxJava is a JVM implementation of ReactiveX, developed by NetFlix, and is very popular among Java developers. In this tutorial, you will learn how to use RxJava bindings for Android, or RxAndroid for short, in your Android projects.

To use RxAndroid in an Android Studio project, add it as a compiledependency in the app module’s build.gradle.

When working with ReactiveX, you will be using observables and observers extensively. You can think of an observable as an object that emits data and an observer as an object that consumes that data. In RxJava and RxAndroid, observers are instances of the Observer interface, and observables are instances of the Observable class.

The Observable class has many static methods, called operators, to create Observable objects. The following code shows you how to use the justoperator to create a very simple Observable that emits a single String.

The observable we just created will emit its data only when it has at least one observer. To create an observer, you create a class that implements the Observer interface. The Observer interface has intuitively named methods to handle the different types of notifications it can receive from the observable. Here’s an observer that can print the String emitted by the observable we created earlier:

To assign an observer to an observable, you should use the subscribe method, which returns a Subscription object. The following code makes myObserverobserve myObservable:

As soon as an observer is added to the observable, it emits its data. Therefore, if you execute the code now, you will see Hello printed in Android Studio’s logcat window.

You might have noticed that we didn’t use the onCompleted and the onErrormethods in myObserver. As these methods are often left unused, you also have the option of using the Action1 interface, which contains a single method named call.

When you pass an instance of Action1 to the subscribe method, the callmethod is invoked whenever the observable emits data.

To detach an observer from its observable while the observable is still emitting data, you can call the unsubscribe method on the Subscription object.

Now that you know how to create observers and observables, let me show you how to use ReactiveX’s operators that can create, transform, and perform other operations on observables. Let’s start by creating a slightly more advanced Observable, one that emits items from an array of Integer objects. To do so, you have to use the from operator, which can generate an Observable from arrays and lists.

When you run this code, you will see each of the numbers of the array printed one after another.

If you’re familiar with JavaScript, Ruby, or Kotlin, you might be familiar with higher-order functions such as map and filter, which can be used when working with arrays. ReactiveX has operators that can perform similar operations on observables. However, because Java 7 doesn’t have lambdas and higher-order functions, we’ll have to do it with classes that simulate lambdas. To simulate a lambda that takes one argument, you will have to create a class that implements the Func1 interface.

Here’s how you can use the map operator to square each item of myArrayObservable:

Note that the call to the map operator returns a new Observable, it doesn’t change the original Observable. If you subscribe to myArrayObservable now, you will receive squares of the numbers.

Operators can be chained. For example, the following code block uses the skip operator to skip the first two numbers, and then the filter operator to ignore odd numbers:

The observers and observables we created in the previous sections worked on a single thread, Android’s UI thread. In this section, I will show you how to use ReactiveX to manage multiple threads and how ReactiveX solves the problem of callback hell.

Assume you have a method named fetchData that can be used to fetch data from an API. Let’s say it accepts a URL as its parameter and returns the contents of the response as a String. The following code snippet shows how it could be used.

This method needs to run on its own thread, because Android does not allow network operations on the UI thread. This means you would either create an AsyncTask or create a Thread that uses a Handler.

With ReactiveX, however, you have a third option that is slightly more concise. Using the subscribeOn and observeOn operators, you can explicitly specify which thread should run the background job and which thread should handle the user interface updates.

The following code creates a custom Observable using the create operator. When you create an Observable in this manner, you have to implement the Observable.OnSubscribe interface and control what it emits by calling the onNextonError, and onCompleted methods yourself.

When the Observable is ready, you can use subscribeOn and observeOn to specify the threads it should use and subscribe to it.

You might still be thinking that the reactive approach isn’t drastically better than using the AsyncTask or Handler classes. You are right, you don’t really need ReactiveX if you have to manage only one background job.

Now consider a scenario that would result in a complex codebase if you used the conventional approach. Let’s say you have to fetch data from two (or more) websites in parallel and update a View only when all the requests have completed. If you follow the conventional approach, you would have to write lots of unnecessary code to make sure that the requests completed without errors.

Consider another scenario in which you have to start a background job only after another background job has completed. Using the conventional approach, this would result in nested callbacks.

With ReactiveX’s operators, both scenarios can be handled with very little code. For example, if you have to use fetchData to fetch the contents of two websites, fore example Google and Yahoo, you would create two Observableobjects, and use the subscribeOn method to make them run on different threads.

To handle the first scenario in which both requests need to run in parallel, you can use the zip operator and subscribe to the Observable it returns.

Similarly, to handle the second scenario, you can use the concat operator to run the threads one after another.

RxAndroid has a class named ViewObservable that makes it easy to handle events associated with View objects. The following code snippet shows you how to create a ViewObservable that can be used to handle the click events of a Button.

You can now subscribe to clicksObservable and use any of the operators you learned about in the previous sections. For example, if you want your app to skip the first four clicks of the button and start responding from the fifth click onwards, you could use the following implementation:

In this tutorial, you learned how to use ReactiveX’s observers, observables, and operators to handle multiple asynchronous operations and events. As working with ReactiveX involves functional, reactive programming, a programming paradigm most Android developers are not used to, don’t be too hard on yourself if you don’t get it right the first time. You should also know that ReactiveX code will be a lot more readable if you use a modern programming language, such as Kotlin, that supports higher-order functions.

To learn more about reactive extensions, I encourage you to browse the resources available at ReactiveX.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.