RxJava; How to emit observables synchronously

后端 未结 4 1131
不知归路
不知归路 2021-01-11 14:33

I want to synchronously emit two Observable objects (which are asynchronous), one after the other where it returns the first emitted Observable object. If the first

4条回答
  •  一个人的身影
    2021-01-11 15:07

    There is no such term as "wait" in reactive programming. You need to think about creating of a data stream, where one Observable could be triggered by another. In your case after receiving token you need to receive account. It could look like this:

    Observable accountObservable = Observable.create(new Observable.OnSubscribe() {
        @Override public void call(Subscriber subscriber) {
            subscriber.onNext(new AccessToken());
            subscriber.onCompleted();
        }
    }).flatMap(accessToken -> Observable.create(new Observable.OnSubscribe() {
        @Override public void call(Subscriber subscriber) {
            subscriber.onNext(new Account(accessToken));
            subscriber.onCompleted();
        }
    }));
    

提交回复
热议问题