Published on

Android Thread: RxJava Knowledge

Authors

There are many ways to manipulate Threads in Android: Thread, ThreadPoolExecutor, HandlerThread or RxJava or Kotlin Coroutines. In this series of articles, I will provide a full range of knowledge to clarify the concepts, usage, advantages and disadvantages of these 3 methods for readers.

What is RxJava?

Concept

RxJava is a library written in Java, with the aim of simplifying the reception and processing of asynchronous multi-Thread logic in a program. RxJava views the logic it processes as a data stream. On these data streams, RxJava provides powerful support for manipulation, called operators.

You can call Data Streams Observable Objects (objects that can be monitored for data changes).

Types of Observable Objects in RxJava

In programming, there are 2 concepts, Hot Stream and Cold Stream, used to refer to types of data streams with their own characteristics.

The classification of Observable Objects in RxJava will also be classified based on one of these 2 types. The classification criteria are based on the difference in the time the object emits an item, and how they manage Observer Objects.

Types of Operators in RxJava

RxJava supports many operators for manipulation and processing. Memorizing all the ways to use RxJava operators is meaningless. Absorb, refer to, and apply them appropriately to your specific problem!

Backpressure in RxJava?

I'll save this for a separate article. Its knowledge is long, magical and difficult to visualize.

What is RxAndroid?

RxAndroid is an extension of RxJava specifically for the Android Platform. It provides integrations for RxJava to work well and stably on the Android operating system.

Question: Why is RxAndroid needed? If there is no RxAndroid, can RxJava be used in Android?

Answer: RxAndroid is needed so that RxJava can work with Android Components. If there is no RxAndroid, RxJava can still work. But it cannot be integrated with Android Components (Fragment, Activity, UI Thread ...)

usersObservable
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread()) // RxAndroid extension
    .subscribe(users -> {
        // Update the UI with the list of users
    });

Some strengths of RxJava compared to the old programming method

RxJava is easy to use and easy to understand for people with a good background in Threading and Multi Threads programming.

  • RxJava uses ThreadPool and is planned in Schedulers, making the code cleaner and more transparent. Schedulers.io, Schedulers.computation. This also helps the written software make good use of resources in the program.
  • RxJava manages subscribing to Observable Objects using Disposable Objects. This means that programmers can actively manage the lifecycle of data streams. Actively avoid memory leaks.
  • RxJava provides an error management solution through the onError callback for the subscriber. Programs written with RxJava will be safer with crashes during processing upstream.
  • RxJava provides a solution to switch threads in an easy-to-understand and easy-to-use way through the 2 methods subscribeOn, observeOn

Some drawbacks of RxJava

Calling it a drawback of RxJava and not a disadvantage because this will always be a problem of multi threading. What I want to convey here is to point out the remaining problems to avoid when working. There really cannot be a perfect solution even if we use any tool for multi threading programming.

  • RxJava uses ThreadPool, but if all Threads in the ThreadPool are being used, RxJava will create more Threads. This inadvertently still causes overhead for the application. Please emphasize that this is not a disadvantage, this is the existing truth in programming. I'm pointing this out so we understand that RxJava is not a panacea for all problems in multi thread Java / Android.
  • Callback hells - of course, because multi thread is callback hells. It's what it's
  • When I think of something, I'll write it here!