retrofit2.0+ RxJava elegant de-duplication avoidance and cancellation requests (11)

Source: Internet
Author: User

tamic/article
Address: http://blog.csdn.net/sk719887916/article/details/54575137

The previous several mainly introduced the basic use of retrofit, combined with Rxjava case, as well as Rxjava combination of retrofit package, including common parameters, local parameter request header add, cache, HTTPS, file up and down, result parsing, exception handling, there are some tricks, Then there is one more key is to cancel the problem.

The combination of the two skills can be clicked to read; http://blog.csdn.net/sk719887916/article/details/52132106
retrofit2.0+ RxJava elegant de-duplication avoidance and cancellation requests

The previous several mainly introduced the basic use of retrofit, combined with Rxjava case, and Rxjava combined with retrofit package, including common parameters, local parameter request header add, cache, HTTPS, file up and down, result parsing, exception handling, etc. Then there is a more critical issue is the cancellation,

Retrofit cancel the Request build API
Call<LoginResult> call = apiService.getData("Tamic", "1234");   call.enqueue(new Callback<LoginResult>() {   @Override   public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {   }   @Override   public void onFailure(Call<LoginResult> call, Throwable t) {   }   });}
Cancel Request

Cancel directly with the call instance

Rxjava Unsubscribe

If you encounter a cancel unsubscribe, you can directly:

subscription.unsubscribe();

Avoid duplication:

if (!subscription.isUnsubscribed()) {    subscription.unsubscribe();}

If you need a non-UI thread, you can specify the thread as the IO thread directly.

 observable.unsubscribeOn(Schedulers.io());
retrofit2.0+ RxJava Cancel Request

The combination of the two skills can be clicked to read; http://blog.csdn.net/sk719887916/article/details/52132106

Address: http://blog.csdn.net/sk719887916/article/details/54575137

Well, there are a lot of situations are using both to do the network framework, then in the combination of how we use to cancel a request, or avoid repeating the request, today we will introduce a little bit about the way I handle

Building an interface

Subscription description of the main management Rxjava. This thing can be understood as a number of events to give the total tag, you go to the ID card to the same receipt, with a receipt you can track the identity of the matter.

/** * Created by Tamic on 2017-01-16. */public interface RxActionManager<T> {void add(T tag, Subscription subscription);void remove(T tag);void cancel(T tag);void cancelAll();}
Specific impl of Rxapimanager

It mainly deals with the actual cancellation request and maintains the Rxjava subscription pool.

/** * Created by tamic on 2017-01-16. */public class Rxapimanager implements rxactionmanager<object> {private static Rxapimanager sinstance = null; Private Arraymap<object, subscription> maps;public static Rxapimanager get () {if (sinstance = = null) {synchronized ( Rxapimanager.class) {if (sinstance = = null) {sinstance = new Rxapimanager ();}}} return sinstance;} @TargetApi (build.version_codes. KITKAT) Private Rxapimanager () {maps = new arraymap<> ();} @TargetApi (build.version_codes. KITKAT) @Overridepublic void Add (Object tag, Subscription Subscription) {maps.put (tag, Subscription);} @TargetApi (build.version_codes. KITKAT) @Overridepublic void Remove (Object tag) {if (!maps.isempty ()) {Maps.remove (tag)}} @TargetApi (build.version_codes. KITKAT) public void RemoveAll () {if (!maps.isempty ()) {maps.clear ();}} @TargetApi (build.version_codes. KITKAT) @Override public void Cancel (Object tag) {if (Maps.isempty ()) {return;} if (maps.get (tag) = = null) {return;} if (!maps.get (tag). isunsubscribed ()) {Maps.get (TAG). isunsubscribed (); Maps.remove (tag);}} @TargetApi (build.version_codes. KITKAT) @Override public void Cancelall () {if (Maps.isempty ()) {return;} set<object> keys = Maps.keyset (); for (Object Apikey:keys) {cancel (ApiKey);}}}
Use Posture

 Subscription of specific requests

   Subscription subscription = Retrofit返回的subscription实例

  Join the RXAPI management pool
  
RxApiManager.get().add("my", subscription);

Cancel

 RxApiManager.get().cancel("my");

Generally we call in the activity's onDestroy() , fragment onDestroyView()

Can also be canceled in OnPause ();

@Overrideprotected void onPause() {    super.onPause();    RxApiManager.get().cancel("my");}

Okay, two lines of code will be able to gracefully cancel the request, try it quickly!

tamic/article
Address: http://blog.csdn.net/sk719887916/article/details/54575137

Download the case list:

Github:https://github.com/tamicer/novate

retrofit2.0+ RxJava elegant de-duplication avoidance and cancellation requests (11)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.