Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse subscriber

In RxJava I have a Subscriber object wich I subscribe on a Observable. Later on (some time after onComplete() has been invoked) I create a new Observable and subscribe with the same Subscriber instance used before. However, that seems not work. Is a subscriber not reusable?

Example:

class Loader extends Subscriber<T> {

   public void load(){
       Observable.just("Foo").subscribe(this);
   }

   public void onComplete(){
     // update UI
   }

}

In my code I would like to instantiate a Loader once, and call load() multiple time, for instance after the user clicks on a refresh button ...

like image 596
sockeqwe Avatar asked Apr 02 '15 14:04

sockeqwe


1 Answers

You cannot reuse Subscriber, because it implements Subscription, which has an isUnsubscribed field which, once set to true, will never become false again, so Subscription is not reusable.

Observer, on the other hand, does not contain any information about the subscription status, so you can reuse it. Each time you subscribe an Observer to an Observable, the RxJava implementation will wrap it inside a new Subscriber for you.

like image 150
Samuel Gruetter Avatar answered Oct 22 '22 20:10

Samuel Gruetter