Retrofit2.0 + RxJava: How to elegantly cancel repeated requests to avoid and cancel requests, retrofit2.0rxjava

Source: Internet
Author: User

Retrofit2.0 + RxJava: How to elegantly cancel repeated requests to avoid and cancel requests, retrofit2.0rxjava
Invalid fit cancel request building API

Call
 
   call = apiService.getData("Tamic", "1234");   call.enqueue(new Callback
  
   () {   @Override   public void onResponse(Call
   
     call, Response
    
      response) {   }   @Override   public void onFailure(Call
     
       call, Throwable t) {   }   });}
     
    
   
  
 
Cancel request

Use the call instance for cancel.

 call.cancel(); 
Rxjava unsubscribe

In case of canceling the subscription, you can directly:

subscription.unsubscribe();

Avoid repetition:

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

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

 observable.unsubscribeOn(Schedulers.io());
Retrofit2.0 + RxJava cancel request

Now, the two are used as the network framework. How can we cancel a request or avoid repeated requests when the two are combined, today, I will introduce some of my solutions.

Build an Interface

It mainly manages the subscribe of rxJava. This stuff can be understood to be the same as the total Tag given by some events. You can follow up on the ID card with the same ID card receipt ticket.

/** * Created by Tamic on 2017-01-16. */public interface RxActionManager
 
   {void add(T tag, Subscription subscription);void remove(T tag);void cancel(T tag);void cancelAll();}
 
The specific impl RxApiManager

It mainly handles real cancellation requests and maintains the rxjava subscription pool.

/** * Created by Tamic on 2017-01-16. */public class RxApiManager implements RxActionManager {private static RxApiManager sInstance = null;private ArrayMap
  
    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).unsubscribed();maps.remove(tag);}}@TargetApi(Build.VERSION_CODES.KITKAT)@Override public void cancelAll() {if (maps.isEmpty()) {return;}Set
    keys = maps.keySet();for (Object apiKey : keys) {cancel(apiKey);}}}
  

 Subdomains of specific requests

   Subpartition = subpartition instance returned by fit

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

Cancel

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

GenerallyOnDestroy (), Fragment'sCalled in onDestroyView ()

It can also be canceled in onPause;

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

Now we can cancel the request elegantly with two codes. Please try it!

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.