RxJava Observable MayBe类

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 com.yiidian;

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();
   }
}

输出结果为:

热门文章

优秀文章