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!