How to cancel a call with RxJava2?










1















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 '18 at 10:29











  • Retrofit version 2.4.0

    – Ebn Zhang
    Nov 11 '18 at 12:42











  • @EbnZhang did you check my answer?

    – Aks4125
    Nov 17 '18 at 7:58















1















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 '18 at 10:29











  • Retrofit version 2.4.0

    – Ebn Zhang
    Nov 11 '18 at 12:42











  • @EbnZhang did you check my answer?

    – Aks4125
    Nov 17 '18 at 7:58













1












1








1








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 '18 at 0:09







Ebn Zhang

















asked Nov 10 '18 at 23:59









Ebn ZhangEbn Zhang

1018




1018












  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.

    – akarnokd
    Nov 11 '18 at 10:29











  • Retrofit version 2.4.0

    – Ebn Zhang
    Nov 11 '18 at 12:42











  • @EbnZhang did you check my answer?

    – Aks4125
    Nov 17 '18 at 7:58

















  • What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.

    – akarnokd
    Nov 11 '18 at 10:29











  • Retrofit version 2.4.0

    – Ebn Zhang
    Nov 11 '18 at 12:42











  • @EbnZhang did you check my answer?

    – Aks4125
    Nov 17 '18 at 7:58
















What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.

– akarnokd
Nov 11 '18 at 10:29





What Retrofit version are you using. Sounds like Retrofit doesn't cancel the upload properly.

– akarnokd
Nov 11 '18 at 10:29













Retrofit version 2.4.0

– Ebn Zhang
Nov 11 '18 at 12:42





Retrofit version 2.4.0

– Ebn Zhang
Nov 11 '18 at 12:42













@EbnZhang did you check my answer?

– Aks4125
Nov 17 '18 at 7:58





@EbnZhang did you check my answer?

– Aks4125
Nov 17 '18 at 7:58












3 Answers
3






active

oldest

votes


















1














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 '18 at 7:08


















0















  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 '18 at 18:57











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

    – Aks4125
    Nov 26 '18 at 8:24


















0














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',
    autoActivateHeartbeat: false,
    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









    1














    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 '18 at 7:08















    1














    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 '18 at 7:08













    1












    1








    1







    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 '18 at 3:05









    AolphnAolphn

    1,1272514




    1,1272514







    • 2





      mCompositeDisposable.clear(); should dispose and clear all Disposables added. I'm pretty sure that disposable.dispose() is called.

      – Ebn Zhang
      Nov 11 '18 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 '18 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 '18 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 '18 at 7:08













    0















    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 '18 at 18:57











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

      – Aks4125
      Nov 26 '18 at 8:24















    0















    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 '18 at 18:57











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

      – Aks4125
      Nov 26 '18 at 8:24













    0












    0








    0








    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 '18 at 9:19

























    answered Nov 12 '18 at 9:13









    Aks4125Aks4125

    2,72111131




    2,72111131












    • 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 '18 at 18:57











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

      – Aks4125
      Nov 26 '18 at 8:24

















    • 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 '18 at 18:57











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

      – Aks4125
      Nov 26 '18 at 8:24
















    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 '18 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 '18 at 18:57













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

    – Aks4125
    Nov 26 '18 at 8:24





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

    – Aks4125
    Nov 26 '18 at 8:24











    0














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






    share|improve this answer



























      0














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






      share|improve this answer

























        0












        0








        0







        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 '18 at 9:48









        ShaonShaon

        250111




        250111



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            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

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ