How to cancel request with retofit2 and RxAndroid

前端 未结 1 1898
灰色年华
灰色年华 2021-01-12 10:26

I am using Retrofit 2.0 and Rx-android to load my APIs. I follow the section RxJava Integration with CallAdapter at this site and it work fine. But, I don\'t kn

相关标签:
1条回答
  • 2021-01-12 11:09

    The only way to cancel RxJava Observable execution - unsubscribe from it. RxJavaCallAdapter will delegate cancel to okhttp client.

    So, you simple do smth like this:

    Subscription subscription = getObservable().subscribe();
    
    //When you want to stop execution
    subscription.unsubsribe();
    

    You can check out the code here. Concretely these lines if code

    final Call<T> call = originalCall.clone();
    
    // Attempt to cancel the call if it is still in-flight on unsubscription.
    subscriber.add(Subscriptions.create(new Action0() {
      @Override public void call() {
        call.cancel();
      }
    }));
    
    0 讨论(0)
提交回复
热议问题