RxJava: Creating easy threading in Android (Kotlin)

Tram Ho

Threading is always a difficult topic in programming. But it is very important in Android. For example, we want to put all the slow processes (eg, get network data, read the data on the drive, ….) in the background progress to avoid overloading. UI thread.

So we can’t help but learn it. But be optimistic when RxJava 2 makes it easier.

Chain diagram of Rx

In the previous blog I mentioned RxJava’s code can be broken down into three sections as follows: Producer, Operators and Consumer. It is possible that Operators are very much as shown below:

Operators

And then, we thought, there are some operations that are slow, and want it to be run on some background thread as below … and the Consumer completes the thread on the main UI thread.

Consumer

If we think about using old style threading code, this is really complicated. It may even be a crazy thing to think about how to make them work. But with RxJava 2 then …

Just use observeOn (… thread …)

We can simply add observeOn(...) function observeOn(...) between the blocks of the string as shown below. And that’s all

observeOn

And the pseudo code will look like the following

It is simple and easy. But…

You will have a question …

What is the Producer component?

If the operations are really slow (like the one below), and we want to put it in the background. So how do we do that?

Just use subscribeOn (… thread …)

Similar to using observeOn(...) we use subscribeOn(...) instead. Unlike using observeOn , here you can use subscribeOn anywhere in the series (just after Producer and before Consumer). Wherever you put it, there is no problem. As long as it is placed somewhere in the string. Tnos is enough to make Producer work on that Thread.

The diagram below shows all. Just put them (the red arrows)

Note: If you put more than one point, in fact it will not be different compared to just placing one point (with a small exception, you may not need to care about that).

subscribeOn

Our code will look similar to the one below

Just once call you have done that. All threads handling the operators will be listened to, until they see the first place observeOn(...) . In another expression, if you don’t have observeOn , then the definitions in RxJava will be executed on threads defined by subscribeOn .

However, in one case, it might look something like the following, where the final component component of the definition code will return to the main thread UI.

uimain

And our code will look similar to the one below

They are similar to the code below, while their nature is similar to the above code. But with the desire that they are easy to see where subscribeOn and where to place observeOn

Simple? But there is a prior warning about subscribeOn j where its location is set to important.

It does not work with Hot Observable

Unfortunately, subscribeOn only works on COLD Observable but does not work on HOT. The reason for this is very simple. RxJava framwork has no similarity when controlling Hot Observable and COLD Observable.

Below is a summary topic of the Hot Observable and COLD Observable.

RxJava 2: Understanding Hot vs. Cold with just vs.. fromCallable

Hope this article is useful to you.

Translation source [RxJava 2: Making threading easy in Android (in Kotlin)] ( https://medium.com/@elye.project/rxjava-2-making-threading-easy-in-android-in-kotlin-603d8342d6c )

Share the news now

Source : Viblo