RxJava Observable MayBe类
精华
小牛编辑
99浏览
2023-03-14
RxJava Observable MayBe类 介绍
MayBe 类表示延迟响应。MayBe observable 可以发出单个成功值或不发出任何值。
RxJava Observable MayBe类 声明
public abstract class Maybe<T>
extends Object
implements MaybeSource<T>
RxJava Observable MayBe类 协议
以下是 MayBe Observable 操作的顺序协议:
onSubscribe (onSuccess | onError | OnComplete)?
RxJava Observable MayBe类 示例
package cn.xnip;
import io.reactivex.Maybe;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableMaybeObserver;
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 = Maybe.just("你好,小牛知识库网")
.delay(2, TimeUnit.SECONDS, Schedulers.io())
.subscribeWith(new DisposableMaybeObserver<String>() {
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onSuccess(String value) {
System.out.println(value);
}
@Override
public void onComplete() {
System.out.println("Done!");
}
});
Thread.sleep(3000);
//start observing
disposable.dispose();
}
}
输出结果为: