如何手动取消/处置RXJava2 Flowable?

时间:2019-05-31 21:36:13

标签: java android rx-java2 flowable filestack

我有一个使用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的可用方法时,我没有看到该选项。

1 个答案:

答案 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