亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何手動取消/處置 RXJava2 Flowable?

我如何手動取消/處置 RXJava2 Flowable?

慕婉清6462132 2022-12-28 10:41:02
我有一個使用依賴于 RX Java 2的FileStack 依賴項io.reactivex.rxjava2:rxjava:2.1.2的 Android 項目。具體來說, . 直到現在這還不是真正的問題,因為我一直無法弄清楚如何具體取消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();}我需要做什么/調用uploadFlowable 以便在用戶通過單擊按鈕取消上傳時我可以手動觸發取消?我看到有人建議調用dispose,但在檢查可用于 Flowable 的可用方法時我沒有看到該選項。
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

原來問題是我試圖處理/取消錯誤的對象。我將代碼調整為以下內容:


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的幫助/留言


查看完整回答
反對 回復 2022-12-28
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號