美文网首页
再忆RxJava---Single,Maybe,Completa

再忆RxJava---Single,Maybe,Completa

作者: 勇敢地追 | 来源:发表于2019-08-25 22:38 被阅读0次

Single

Single和Observable类似,但是它主要处理单个数值,它只会返回onSuccess和onError,没有onComplete

        Single.just("Hello World")
                .delay(10, TimeUnit.SECONDS, new IoScheduler())
                .subscribeWith(new DisposableSingleObserver<String>() {
                    @Override
                    public void onSuccess(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
                // .subscribe(new SingleObserver<String>() {});---此方法没有返回值,所以不用

Single默认只是调用onSuccess。可以在Single.create中手动调用onError

Maybe

Maybe,也就是“或许”的意思。它是一种延迟计算,要么发射单个数据,要么不发送数据,要么发送exception
onSuccess,onError和onComplete三个方法,Observable只有onError和onComplete不能同时存在,但是Maybe完全互斥(DelayMaybeObserver的run方法有)
和Single一样不能用subscribe方法,因为没有返回值

        Maybe.just("Hello World")
                .delay(10, TimeUnit.SECONDS, new IoScheduler())
                .subscribeWith(new DisposableMaybeObserver<String>() {
                    @Override
                    public void onSuccess(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

Completable

CompletableDelay内部类Delay的run方法可以知道,要么onError,要么onComplete
和Single一样不能用subscribe方法,因为没有返回值

        Completable.complete()
                .delay(10, TimeUnit.SECONDS, new IoScheduler())
                .subscribeWith(new DisposableCompletableObserver() {
                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });

相关文章

  • 再忆RxJava---Single,Maybe,Completa

    Single Single和Observable类似,但是它主要处理单个数值,它只会返回onSuccess和onE...

  • 膳食

    Pension completa

  • Maybe not maybe

    There are some things I haven't considered clearly I just...

  • Maybe not maybe

    There are some things I haven't considered clearly I just...

  • Maybe yes maybe no

    觉得你是爱我的 所有平常日子里的孤单都有了放飞的翅膀 所有阳光不曾照耀的角落都回了春 觉得你不是爱我的 所有点点滴...

  • 《地图册》之一 |《博尔赫斯诗歌总集》

    [转载2019年3月11日公众号CopyMachine] 西语原版的Poesía Completa并未收入《地图册...

  • Maybe not

    Things are maybe not as good as they should be Maybe not ...

  • Imagination

    Maybe you don't know me Maybe I passed you just now Maybe...

  • nice catch

    this is fantasy well maybe maybe this is another writing ...

  • Promise

    Maybe what I need is not an unintentional promise ,maybe ...

网友评论

      本文标题:再忆RxJava---Single,Maybe,Completa

      本文链接:https://www.haomeiwen.com/subject/nnxlectx.html