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?
android retrofit rx-java2
add a comment |
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?
android retrofit rx-java2
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
add a comment |
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?
android retrofit rx-java2
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
android retrofit rx-java2
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
use disposable.dipose()
for canceling your call.
2
mCompositeDisposable.clear();
should dispose and clear allDisposable
s added. I'm pretty sure thatdisposable.dispose()
is called.
– Ebn Zhang
Nov 11 at 7:08
add a comment |
up vote
0
down vote
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 at 18:57
@EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
– Aks4125
yesterday
add a comment |
up vote
0
down vote
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
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.
2
mCompositeDisposable.clear();
should dispose and clear allDisposable
s added. I'm pretty sure thatdisposable.dispose()
is called.
– Ebn Zhang
Nov 11 at 7:08
add a comment |
up vote
1
down vote
use disposable.dipose()
for canceling your call.
2
mCompositeDisposable.clear();
should dispose and clear allDisposable
s added. I'm pretty sure thatdisposable.dispose()
is called.
– Ebn Zhang
Nov 11 at 7:08
add a comment |
up vote
1
down vote
up vote
1
down vote
use disposable.dipose()
for canceling your call.
use disposable.dipose()
for canceling your call.
answered Nov 11 at 3:05
Aolphn
1,1022514
1,1022514
2
mCompositeDisposable.clear();
should dispose and clear allDisposable
s added. I'm pretty sure thatdisposable.dispose()
is called.
– Ebn Zhang
Nov 11 at 7:08
add a comment |
2
mCompositeDisposable.clear();
should dispose and clear allDisposable
s added. I'm pretty sure thatdisposable.dispose()
is called.
– Ebn Zhang
Nov 11 at 7:08
2
2
mCompositeDisposable.clear();
should dispose and clear all Disposable
s added. I'm pretty sure that disposable.dispose()
is called.– Ebn Zhang
Nov 11 at 7:08
mCompositeDisposable.clear();
should dispose and clear all Disposable
s added. I'm pretty sure that disposable.dispose()
is called.– Ebn Zhang
Nov 11 at 7:08
add a comment |
up vote
0
down vote
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 at 18:57
@EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
– Aks4125
yesterday
add a comment |
up vote
0
down vote
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 at 18:57
@EbnZhang in this case futurestud.io/tutorials/retrofit-2-cancel-requests.
– Aks4125
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
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 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
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 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
add a comment |
up vote
0
down vote
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
up vote
0
down vote
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
add a comment |
up vote
0
down vote
up vote
0
down vote
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
Add RxJava to CompositeDisposable
Then in onStop()
use disposableRxJava.dispose()
answered Nov 13 at 9:48
Shaon
235110
235110
add a comment |
add a comment |
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 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