How to cancel a call with RxJava2?











up vote
1
down vote

favorite












I'm using RxJava2 and Retrofit. In my fragment, I make a request to upload a local file:



Disposable disposable = mApi.requestUpload(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
toast("success");
}, throwable -> {
toast("failed");
});
mCompositeDisposable.add(disposable);


Then, clear all disposables in onDestroyView()



@Override
public void onDestroyView() {
mCompositeDisposable.clear();
super.onDestroyView();
}


But I use Charles to view all requests and find that the request is still executing after I finish the fragment. The file is still uploaded successfully after a time.



How can I cancel the call when fragment closed?










share|improve this question
























  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
    – akarnokd
    Nov 11 at 10:29










  • Retrofit version 2.4.0
    – Ebn Zhang
    Nov 11 at 12:42










  • @EbnZhang did you check my answer?
    – Aks4125
    Nov 17 at 7:58















up vote
1
down vote

favorite












I'm using RxJava2 and Retrofit. In my fragment, I make a request to upload a local file:



Disposable disposable = mApi.requestUpload(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
toast("success");
}, throwable -> {
toast("failed");
});
mCompositeDisposable.add(disposable);


Then, clear all disposables in onDestroyView()



@Override
public void onDestroyView() {
mCompositeDisposable.clear();
super.onDestroyView();
}


But I use Charles to view all requests and find that the request is still executing after I finish the fragment. The file is still uploaded successfully after a time.



How can I cancel the call when fragment closed?










share|improve this question
























  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
    – akarnokd
    Nov 11 at 10:29










  • Retrofit version 2.4.0
    – Ebn Zhang
    Nov 11 at 12:42










  • @EbnZhang did you check my answer?
    – Aks4125
    Nov 17 at 7:58













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using RxJava2 and Retrofit. In my fragment, I make a request to upload a local file:



Disposable disposable = mApi.requestUpload(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
toast("success");
}, throwable -> {
toast("failed");
});
mCompositeDisposable.add(disposable);


Then, clear all disposables in onDestroyView()



@Override
public void onDestroyView() {
mCompositeDisposable.clear();
super.onDestroyView();
}


But I use Charles to view all requests and find that the request is still executing after I finish the fragment. The file is still uploaded successfully after a time.



How can I cancel the call when fragment closed?










share|improve this question















I'm using RxJava2 and Retrofit. In my fragment, I make a request to upload a local file:



Disposable disposable = mApi.requestUpload(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
toast("success");
}, throwable -> {
toast("failed");
});
mCompositeDisposable.add(disposable);


Then, clear all disposables in onDestroyView()



@Override
public void onDestroyView() {
mCompositeDisposable.clear();
super.onDestroyView();
}


But I use Charles to view all requests and find that the request is still executing after I finish the fragment. The file is still uploaded successfully after a time.



How can I cancel the call when fragment closed?







android retrofit rx-java2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 0:09

























asked Nov 10 at 23:59









Ebn Zhang

1018




1018












  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
    – akarnokd
    Nov 11 at 10:29










  • Retrofit version 2.4.0
    – Ebn Zhang
    Nov 11 at 12:42










  • @EbnZhang did you check my answer?
    – Aks4125
    Nov 17 at 7:58


















  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
    – akarnokd
    Nov 11 at 10:29










  • Retrofit version 2.4.0
    – Ebn Zhang
    Nov 11 at 12:42










  • @EbnZhang did you check my answer?
    – Aks4125
    Nov 17 at 7:58
















What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
– akarnokd
Nov 11 at 10:29




What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.
– akarnokd
Nov 11 at 10:29












Retrofit version 2.4.0
– Ebn Zhang
Nov 11 at 12:42




Retrofit version 2.4.0
– Ebn Zhang
Nov 11 at 12:42












@EbnZhang did you check my answer?
– Aks4125
Nov 17 at 7:58




@EbnZhang did you check my answer?
– Aks4125
Nov 17 at 7:58












3 Answers
3






active

oldest

votes

















up vote
1
down vote













use disposable.dipose() for canceling your call.






share|improve this answer

















  • 2




    mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
    – Ebn Zhang
    Nov 11 at 7:08


















up vote
0
down vote















  1. Define Disposable



    private io.reactivex.disposables.Disposable mDisposable;




  2. assign to your service



    mService.getResults(query)
    .observeOn(AndroidSchedulers.from(Looper.getMainLooper(), true))
    .subscribeOn(Schedulers.io())
    .subscribe(new SingleObserver<Response<Model>>() {
    @Override
    public void onSubscribe(Disposable d) {
    /* required */
    mDisposable = d;
    }

    @Override
    public void onSuccess(Response<Model> response) {
    dismissProgress();
    }

    @Override
    public void onError(Throwable e) {

    }
    });



  3. call below line to dismiss/terminate ongoing API call



     if (mDisposable != null)
    mDisposable.dispose();



There you go.



UPDATE



in your case, it should be



@Override
public void onDestroyView() {
if (mCompositeDisposable!= null) // in case if you required-> if (mCompositeDisposable!= null && !mCompositeDisposable.isDisposed())
mCompositeDisposable.dispose();
super.onDestroyView();
}





share|improve this answer























  • Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
    – Ebn Zhang
    Nov 24 at 18:57










  • @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
    – Aks4125
    yesterday


















up vote
0
down vote













Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244608%2fhow-to-cancel-a-call-with-rxjava2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    use disposable.dipose() for canceling your call.






    share|improve this answer

















    • 2




      mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
      – Ebn Zhang
      Nov 11 at 7:08















    up vote
    1
    down vote













    use disposable.dipose() for canceling your call.






    share|improve this answer

















    • 2




      mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
      – Ebn Zhang
      Nov 11 at 7:08













    up vote
    1
    down vote










    up vote
    1
    down vote









    use disposable.dipose() for canceling your call.






    share|improve this answer












    use disposable.dipose() for canceling your call.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 3:05









    Aolphn

    1,1022514




    1,1022514








    • 2




      mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
      – Ebn Zhang
      Nov 11 at 7:08














    • 2




      mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
      – Ebn Zhang
      Nov 11 at 7:08








    2




    2




    mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
    – Ebn Zhang
    Nov 11 at 7:08




    mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.
    – Ebn Zhang
    Nov 11 at 7:08












    up vote
    0
    down vote















    1. Define Disposable



      private io.reactivex.disposables.Disposable mDisposable;




    2. assign to your service



      mService.getResults(query)
      .observeOn(AndroidSchedulers.from(Looper.getMainLooper(), true))
      .subscribeOn(Schedulers.io())
      .subscribe(new SingleObserver<Response<Model>>() {
      @Override
      public void onSubscribe(Disposable d) {
      /* required */
      mDisposable = d;
      }

      @Override
      public void onSuccess(Response<Model> response) {
      dismissProgress();
      }

      @Override
      public void onError(Throwable e) {

      }
      });



    3. call below line to dismiss/terminate ongoing API call



       if (mDisposable != null)
      mDisposable.dispose();



    There you go.



    UPDATE



    in your case, it should be



    @Override
    public void onDestroyView() {
    if (mCompositeDisposable!= null) // in case if you required-> if (mCompositeDisposable!= null && !mCompositeDisposable.isDisposed())
    mCompositeDisposable.dispose();
    super.onDestroyView();
    }





    share|improve this answer























    • Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
      – Ebn Zhang
      Nov 24 at 18:57










    • @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
      – Aks4125
      yesterday















    up vote
    0
    down vote















    1. Define Disposable



      private io.reactivex.disposables.Disposable mDisposable;




    2. assign to your service



      mService.getResults(query)
      .observeOn(AndroidSchedulers.from(Looper.getMainLooper(), true))
      .subscribeOn(Schedulers.io())
      .subscribe(new SingleObserver<Response<Model>>() {
      @Override
      public void onSubscribe(Disposable d) {
      /* required */
      mDisposable = d;
      }

      @Override
      public void onSuccess(Response<Model> response) {
      dismissProgress();
      }

      @Override
      public void onError(Throwable e) {

      }
      });



    3. call below line to dismiss/terminate ongoing API call



       if (mDisposable != null)
      mDisposable.dispose();



    There you go.



    UPDATE



    in your case, it should be



    @Override
    public void onDestroyView() {
    if (mCompositeDisposable!= null) // in case if you required-> if (mCompositeDisposable!= null && !mCompositeDisposable.isDisposed())
    mCompositeDisposable.dispose();
    super.onDestroyView();
    }





    share|improve this answer























    • Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
      – Ebn Zhang
      Nov 24 at 18:57










    • @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
      – Aks4125
      yesterday













    up vote
    0
    down vote










    up vote
    0
    down vote











    1. Define Disposable



      private io.reactivex.disposables.Disposable mDisposable;




    2. assign to your service



      mService.getResults(query)
      .observeOn(AndroidSchedulers.from(Looper.getMainLooper(), true))
      .subscribeOn(Schedulers.io())
      .subscribe(new SingleObserver<Response<Model>>() {
      @Override
      public void onSubscribe(Disposable d) {
      /* required */
      mDisposable = d;
      }

      @Override
      public void onSuccess(Response<Model> response) {
      dismissProgress();
      }

      @Override
      public void onError(Throwable e) {

      }
      });



    3. call below line to dismiss/terminate ongoing API call



       if (mDisposable != null)
      mDisposable.dispose();



    There you go.



    UPDATE



    in your case, it should be



    @Override
    public void onDestroyView() {
    if (mCompositeDisposable!= null) // in case if you required-> if (mCompositeDisposable!= null && !mCompositeDisposable.isDisposed())
    mCompositeDisposable.dispose();
    super.onDestroyView();
    }





    share|improve this answer
















    1. Define Disposable



      private io.reactivex.disposables.Disposable mDisposable;




    2. assign to your service



      mService.getResults(query)
      .observeOn(AndroidSchedulers.from(Looper.getMainLooper(), true))
      .subscribeOn(Schedulers.io())
      .subscribe(new SingleObserver<Response<Model>>() {
      @Override
      public void onSubscribe(Disposable d) {
      /* required */
      mDisposable = d;
      }

      @Override
      public void onSuccess(Response<Model> response) {
      dismissProgress();
      }

      @Override
      public void onError(Throwable e) {

      }
      });



    3. call below line to dismiss/terminate ongoing API call



       if (mDisposable != null)
      mDisposable.dispose();



    There you go.



    UPDATE



    in your case, it should be



    @Override
    public void onDestroyView() {
    if (mCompositeDisposable!= null) // in case if you required-> if (mCompositeDisposable!= null && !mCompositeDisposable.isDisposed())
    mCompositeDisposable.dispose();
    super.onDestroyView();
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 9:19

























    answered Nov 12 at 9:13









    Aks4125

    2,44611131




    2,44611131












    • Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
      – Ebn Zhang
      Nov 24 at 18:57










    • @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
      – Aks4125
      yesterday


















    • Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
      – Ebn Zhang
      Nov 24 at 18:57










    • @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
      – Aks4125
      yesterday
















    Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
    – Ebn Zhang
    Nov 24 at 18:57




    Thanks for your answer! I already use code like your answer, but still not work. Dispose all Disposable only unsubscribe the result of current call, but not really stop uploading file. And I found that cancel a Http call is not reliable, so maybe my question is not about RxJava......
    – Ebn Zhang
    Nov 24 at 18:57












    @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
    – Aks4125
    yesterday




    @EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
    – Aks4125
    yesterday










    up vote
    0
    down vote













    Add RxJava to CompositeDisposable
    Then in onStop()
    use disposableRxJava.dispose()






    share|improve this answer

























      up vote
      0
      down vote













      Add RxJava to CompositeDisposable
      Then in onStop()
      use disposableRxJava.dispose()






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Add RxJava to CompositeDisposable
        Then in onStop()
        use disposableRxJava.dispose()






        share|improve this answer












        Add RxJava to CompositeDisposable
        Then in onStop()
        use disposableRxJava.dispose()







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 9:48









        Shaon

        235110




        235110






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244608%2fhow-to-cancel-a-call-with-rxjava2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values