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 ...
Use
Observable::concat
to create an Observable that emits the items from several Observables in sequence. Subscribe that.You cannot reuse
Subscriber
, because it implementsSubscription
, which has anisUnsubscribed
field which, once set totrue
, will never becomefalse
again, soSubscription
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 anObserver
to anObservable
, the RxJava implementation will wrap it inside anew Subscriber
for you.