Copyright Notice: This article is the original article of the blogger, without the permission of the blogger may not be reproduced.
1. Use Unsubscribe Manager: Compositesubscription, let Compositesubscription hold all requests. Unified cancellation.
This code is the public class Loginpresenter implements basepresenter{private userlogincontract mview;//The current page used in the MVP Priva Te compositesubscription msubscription;//manages all subscriptions to public loginpresenter (Userlogincontract mview) {This.mview
= Mview;
This.mView.setPresenter (this);
This.msubscription = new Compositesubscription (); /** * Get class */public void GetClassInfo () {hashmap<string, string> map = new Hashma
P<> ();
Map.put ("id", "3"); Subscription i = apimanger.getintance (). Api.getclass (map). Flatmap (New Func1<basebean<list<schoo Lclass>>, observable<list<schoolclass>>> () {@Override public
Observable<list<schoolclass>> Call (basebean<list<schoolclass>> ListBaseBean) {
return new Apiinterceptor (). Flatresult (Listbasebean);
}
}) . Compose (Rxschedulers.<list<schoolclass>>io_main ()). Subscribe (New Subscriber<list
<SchoolClass>> () {@Override public void oncompleted () {
Get data to complete mview.loginsuccess (); @Override public void OnError (Throwable e) {Apiexceptio
n apiexception = (apiexception) e;
Mview.showtoast (Apiexception.getrespmsg ());
@Override public void OnNext (list<schoolclass> schoolclasses) {
Get the data, on the page can be data binding operations}}); This.msubscription.add (i)//join the subscription in the Admin collection} @Override public void OnDestroy () {//unsubscribe at the end of the activity lifecycle
To remove the reference to the context if (msubscription!= null) {this.msubscription.unsubscribe (); }} public class Baseactivity extends Appcompatactivity {public Basepresenter presenter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
} @Override protected void OnDestroy () {Super.ondestroy ();
Presenter.ondestroy (); }
}
2. Using the Rxlifecycle Third-party Library, complete the events of observable release and the current component bindings to achieve lifecycle synchronization. Observable subscriptions are automatically canceled at the end of the component life cycle.
Project Dependencies:
Rxlifecycle lifecycle Synchronization
compile ' com.trello:rxlifecycle:0.6.1 '
compile ' com.trello:rxlifecycle-components:0. 6.1 '
rxlifecycle Use
activity/fragment need to inherit rxappcompatactivity/rxfragment, currently supported by Rxappcompatactivity, Rxfragment, RxDialogFragment, Rxfragmentactivity.
one, Bindtolifecycle () method
in the subclass, the compose operator in observable is invoked, completes the observable release event and the current component binding, and implements lifecycle synchronization. Thus, the observable subscription is automatically canceled when the current component lifecycle ends.
Observable.interval (1, timeunit.seconds)
. Compose (This.bindtolifecycle ())
. Subscribe (New Action1<long > () {
@Override public
void call (Long num) {
log.i (TAG, " " +num);
}
});
Second, Binduntilevent () method
Use the Activityevent class, where the Create, START, RESUME, PAUSE, STOP, destroy, respectively, correspond to the methods within the lifecycle. Use Binduntilevent to specify which life cycle method calls to unsubscribe.
Observable.interval (1, timeunit.seconds)
. Compose (This.binduntilevent (activityevent.pause))
. Subscribe (MSUB);
3. Unsubscribe yourself: Subscription.unsubscribe ();
public abstract class Basefragment extends Fragment {protected Subscription;
@Override public void Ondestroyview () {Super.ondestroyview ();
Unsubscribe ();
} protected void Unsubscribe () {if (subscription!= null &&!subscription.isunsubscribed ()) {
Subscription.unsubscribe (); }
}