我有一个使用FileStack dependency且依赖于RX Java 2的Android项目。具体地说,是io.reactivex.rxjava2:rxjava:2.1.2
。到目前为止,这还不是真正的问题,因为我一直无法弄清楚如何专门取消Flowable。
我已经实现了
这是我的代码如下:
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();
}
我需要对
upload
Flowable执行/调用什么操作,以便当用户通过单击按钮取消上载时可以手动触发取消?我看到有人recommending呼叫dispose
,但是在检查可用于Flowable的可用方法时没有看到该选项。 最佳答案
原来问题是我正在尝试处置/取消错误的对象。我将代码调整为以下内容:
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;
}
}
并且能够使其正常运行。本质上,您需要针对
dispose()
对象而不是dispose
调用Flowable
方法。感谢您的帮助/消息jschuss
关于java - 如何手动取消/处置RXJava2 Flowable?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56401968/