.lift(SubscriptionUtils.composite(mCompositeSubscription)) and forget about subscriptions but composite one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rx.Observable; | |
import rx.Subscriber; | |
import rx.functions.Action0; | |
import rx.subscriptions.CompositeSubscription; | |
import rx.subscriptions.Subscriptions; | |
public class SubscriptionUtils { | |
/** | |
* Automatically adds to / removes from specified {@link CompositeSubscription} when subscribe / unsubscribe is called. | |
* Useful when subscribing observable repeatedly, as you do not need to manipulate CompositeSubscription manually. | |
*/ | |
public static <T> Observable.Operator<T, T> composite(CompositeSubscription compositeSubscription) { | |
return new Observable.Operator<T, T>() { | |
@Override | |
public Subscriber<? super T> call(Subscriber<? super T> subscriber) { | |
compositeSubscription.add(subscriber); | |
subscriber.add(Subscriptions.create(new Action0() { | |
@Override | |
public void call() { | |
compositeSubscription.remove(subscriber); | |
} | |
})); | |
return subscriber; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please ensure to call lift() on just before subscribe() call.
Refer trello/RxLifecycle#19 .