How to cancel a call with RxJava2?
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?
add a comment |
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?
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
add a comment |
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?
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?
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
use disposable.dipose() for canceling your call.
2
mCompositeDisposable.clear();should dispose and clear allDisposables added. I'm pretty sure thatdisposable.dispose()is called.
– Ebn Zhang
Nov 11 '18 at 7:08
add a comment |
Define Disposable
private io.reactivex.disposables.Disposable mDisposable;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)
);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();
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
add a comment |
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
use disposable.dipose() for canceling your call.
2
mCompositeDisposable.clear();should dispose and clear allDisposables added. I'm pretty sure thatdisposable.dispose()is called.
– Ebn Zhang
Nov 11 '18 at 7:08
add a comment |
use disposable.dipose() for canceling your call.
2
mCompositeDisposable.clear();should dispose and clear allDisposables added. I'm pretty sure thatdisposable.dispose()is called.
– Ebn Zhang
Nov 11 '18 at 7:08
add a comment |
use disposable.dipose() for canceling your call.
use disposable.dipose() for canceling your call.
answered Nov 11 '18 at 3:05
AolphnAolphn
1,1272514
1,1272514
2
mCompositeDisposable.clear();should dispose and clear allDisposables added. I'm pretty sure thatdisposable.dispose()is called.
– Ebn Zhang
Nov 11 '18 at 7:08
add a comment |
2
mCompositeDisposable.clear();should dispose and clear allDisposables added. I'm pretty sure thatdisposable.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
add a comment |
Define Disposable
private io.reactivex.disposables.Disposable mDisposable;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)
);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();
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
add a comment |
Define Disposable
private io.reactivex.disposables.Disposable mDisposable;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)
);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();
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
add a comment |
Define Disposable
private io.reactivex.disposables.Disposable mDisposable;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)
);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();
Define Disposable
private io.reactivex.disposables.Disposable mDisposable;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)
);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();
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
add a comment |
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
add a comment |
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
answered Nov 13 '18 at 9:48
ShaonShaon
250111
250111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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