Replace callbacks with observables from RxJava

后端 未结 4 1765
醉话见心
醉话见心 2020-12-09 02:17

Im using listeners as callbacks to observe asynchronous operations with Android, but I think that could be great replacing this listeners with RxJava, Im new using this libr

4条回答
  •  情歌与酒
    2020-12-09 02:55

    You are looking for Completable.create:

    Completable: Represents a deferred computation without any value but only indication for completion or exception. The class follows a similar event pattern as Reactive-Streams: onSubscribe (onError|onComplete)?

    Completable.create(subscriber -> {
        object.getData(new OnResponseListener() {
            @Override
            public void onSuccess() {
               subscriber.onCompleted();
            }
    
            @Override
            public void onError() {
               subscriber.onError(* put appropriate Throwable here *);
            }
        }
    })
    ...//apply Schedulers
    .subscribe((() -> *success*), (throwable -> *error*));
    

提交回复
热议问题