KG -
KG -

Reputation: 7200

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();
}

Upvotes: 17

Views: 3078

Answers (2)

Marcin Koziński
Marcin Koziński

Reputation: 11074

It doesn't exist anymore since 1.0 release of RxAndroid. I guess you could say it's deprecated or discontinued. I don't think it's a good idea to use this anymore.

Upvotes: 1

Jonas Lüthke
Jonas Lüthke

Reputation: 1480

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.

Upvotes: 6

Related Questions