Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should one consider using AndroidObservables in RxJava

As i understand AndroidObservable helps ensure that :

  1. a Subscriber always observes on the main thread
  2. when a fragment/activity is detached/stopped, then the observation stops immediately, and framework related components (like ui textviews etc.) are not updated.

However, in order to ensure that the context is released (preventing leakage), most examples I see typically say that you have to anyway do an .unsubscribe onDestroyView/onDestroy, which essentially halts the subscription, and prevents the subscriber from receiving these updates anyway.

So my question is:

Is there any other advantage to using AndroidObservables, if i manually indicate that the subscription should happen on the main thread, by way of .observeOn(AndroidSchedulers.mainThread() ?

Is there any difference in the below two approaches?

_subscription1 = AndroidObservable.bindFragment(MyFragment.this, myCustomAwesomeObservable()) //
                           .subscribeOn(Schedulers.io()) //
                           .subscribe(...);


_subscription2 =  myCustomAwesomeObservable()
                           .subscribeOn(Schedulers.io()) //
                           .observeOn(AndroidSchedulers.mainThread()) //
                           .subscribe(...);


@Override
public void onDestroyView() {
    _subscription1.unsubscribe();
    _subscription2.unsubscribe();
    super.onDestroyView();
}
like image 500
Kaushik Gopal Avatar asked Sep 02 '14 07:09

Kaushik Gopal


People also ask

What is Observable in RxJava Android?

RxJava is a Java VM implementation of ReactiveX a library for composing asynchronous and event-based programs by using observable sequences. The building blocks of RxJava are Observables and Subscribers. Observable is used for emitting items and Subscriber is used for consuming those items.

Why we are using RxJava in Android?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.

What is use of Observable in Android?

Observability refers to the capability of an object to notify others about changes in its data. The Data Binding Library allows you to make objects, fields, or collections observable. Any plain-old object can be used for data binding, but modifying the object doesn't automatically cause the UI to update.

Why coroutines are better than RxJava?

The reason is coroutines makes it easier to write async code and operators just feels more natural to use. As a bonus, Flow operators are all kotlin Extension Functions, which means either you, or libraries, can easily add operators and they will not feel weird to use (in RxJava observable.


1 Answers

You are right. What AndroidObservable.bindFragment currently does is:

This helper will schedule the given sequence to be observed on the main UI thread and ensure that no notifications will be forwarded to the activity in case it is scheduled to finish.

-- part of the source code comment

So, it does not really make a difference which of the implementations you use.

But, still it's a good idea to use the AndroidObservable as additional functionality could be added in the future.

like image 87
Jonas Lüthke Avatar answered Sep 21 '22 04:09

Jonas Lüthke