RxJava Observable Completable类

RxJava Observable Completable类 介绍

Completable 类表示延迟响应。Completable observable 可以指示成功完成或错误。

RxJava Observable Completable类 声明

public abstract class Completable
extends Object
implements CompletableSource

RxJava Observable Completable类 协议

以下是 Completable Observable 操作的顺序协议:

onSubscribe (onError | onComplete)?

RxJava Observable Completable类 示例

package com.yiidian;

import io.reactivex.Completable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableCompletableObserver;
import io.reactivex.schedulers.Schedulers;

import java.util.concurrent.TimeUnit;

public class ObservableTester {
   public static void main(String[] args) throws InterruptedException {

      //Create an observer
      Disposable disposable = Completable.complete()
         .delay(2, TimeUnit.SECONDS, Schedulers.io())
         .subscribeWith(new DisposableCompletableObserver() {
         @Override
         public void onError(Throwable e) { 
            e.printStackTrace();
         }
         @Override
         public void onStart() {
            System.out.println("Started!");
         }
         @Override
         public void onComplete() {
            System.out.println("Done!");
         }
      }); 
      Thread.sleep(3000);
      //start observing
      disposable.dispose();
   }
}

输出结果为:

热门文章

优秀文章