RxJava; How to send observable quantity synchronously

I want to issue two observable objects synchronously (they are asynchronous) and return the first issued observable object one by one If the first fails, the second should not be issued

Suppose we have an observable signing user, and another observable automatically selects the user's account after logging in

This is what I tried:

public Observable<AccessToken> signInAndSelectAccount(String username,String password)
{

    Observable<AccessToken> ob1 = ...; // Sign in.
    Observable<Account> ob2 = ...; // Select account.


    return Observable.zip(
            ob1,ob2,new Func2<AccessToken,Account,AccessToken>() {
                @Override
                public AccessToken call(AccessToken accessToken,Account account)
                {
                     return accessToken;
                }
            });
}

Unfortunately, this doesn't work for my use case It will issue / call two observable in parallel starting with "ob1"

Has anyone encountered a similar use case? Or do you have an idea about how to make observables wait for each other in a synchronous way, where you can return the first one?

Thank you in advance

Solution

There is no such term as "wait" in reactive programming You need to consider creating data streams where one observable can be triggered by another After you receive the token, you need to receive the account It might look like this:

Observable<Account> accountObservable = Observable.create(new Observable.OnSubscribe<AccessToken>() {
    @Override public void call(Subscriber<? super AccessToken> subscriber) {
        subscriber.onNext(new AccessToken());
        subscriber.onCompleted();
    }
}).flatMap(accessToken -> Observable.create(new Observable.OnSubscribe<Account>() {
    @Override public void call(Subscriber<? super Account> subscriber) {
        subscriber.onNext(new Account(accessToken));
        subscriber.onCompleted();
    }
}));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>