我有一个使用FileStack dependency的Android项目,该项目依赖于RX Java2。具体地说,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
可变对象进行调用,以便当用户通过单击按钮取消上传操作时可以手动触发取消操作?我看到有人{{ 3}}调用dispose
,但在检查可用于Flowable的可用方法时,我没有看到该选项。
答案 0 :(得分:0)
原来的问题是我正在尝试处置/取消错误的对象。我将代码调整为以下内容:
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