I have an Android project that is using the FileStack dependency that is relying on RX Java 2. Specifically, io.reactivex.rxjava2:rxjava:2.1.2
. This has not really been a problem up until now as I have been unable to figure out how to specifically cancel a Flowable.
I have implemented the
Here is my code below:
private Flowable<Progress<FileLink>> upload;
private void myMethod(){
upload = new Client(myConfigOptionsHere)
.uploadAsync(filePath, false, storageOptions);
upload.doOnNext(progress -> {
double progressPercent = progress.getPercent();
if(progressPercent > 0){
//Updating progress here
}
if (progress.getData() != null) {
//Sending successful upload callback here
}
})
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
//Logging he complete action here
}
})
.doOnCancel(new Action() {
@Override
public void run() throws Exception {
//Logging the cancel here
}
})
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) throws Exception {
//Logging the error here
}
})
.subscribe();
}
public void cancelUpload(){
//What do I do here to manually stop the upload Flowable?
//IE upload.cancel();
}
What do I need to do / call against the upload
Flowable so that I can manually trigger a cancel when the user cancels the upload via a button click? I see people recommending calling dispose
, but I don't see that option when checking the available methods to use against the Flowable.
Turns out the issue was that I was attempting to dispose of / cancel the wrong object. I adjusted my code to the following:
private Disposable disposable;
private void myMethod(){
Flowable<Progress<FileLink>> upload = new Client(myConfigOptionsHere)
.uploadAsync(filePath, false, storageOptions);
this.disposable = upload.doOnNext(progress -> {
double progressPercent = progress.getPercent();
if(progressPercent > 0){
//Updating progress here
}
if (progress.getData() != null) {
//Sending successful upload callback here
}
})
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
//Logging he complete action here
}
})
.doOnCancel(new Action() {
@Override
public void run() throws Exception {
//Logging the cancel here
}
})
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) throws Exception {
//Logging the error here
}
})
.subscribe();
}
public void cancelUpload(){
if(this.disposable != null){
this.disposable.dispose();
this.disposable = null;
}
}
and was able to get it working just fine. Essentially you need to call the dispose()
method against the dispose
object, not the Flowable
.
Thanks for the assistance / message jschuss