I create an Observable from a long running operation + callback like this:
public Observable<API> login(){
return Observable.create(new Observable.OnSubscribe<API>() {
@Override
public void call(final Subscriber<? super API> subscriber) {
API.login(new SimpleLoginListener() {
@Override
public void onLoginSuccess(String token) {
subscriber.onNext(API.from(token));
subscriber.onCompleted();
}
@Override
public void onLoginFailed(String reason) {
subscriber.onNext(API.error());
subscriber.onCompleted();
}
});
}
})
}
A successfully logged-in api is the pre-condition for multiple other operations like api.getX()
, api.getY()
so I thought I could chain these operation with RxJava and flatMap
like this (simplified): login().getX()
or login().getY()
.
My biggest problem is now, that I don't have control over when login(callback)
is executed. However I want to be able to reuse the login result for all calls.
This means: the wrapped login(callback)
call should be executed only once. The result should then be used for all following calls.
It seems the result would be similar to a queue that aggregates subscribers and then shares the result of the first execution.
What is the best way to achieve this? Am I missing a simpler alternative?
I tried code from this question and experiemented with cache(), share(), publish(), refCount()
etc. but the wrapped function is called 3x when I do this for all of the mentioned operators:
apiWrapper.getX();
apiWrapper.getX();
apiWrapper.getY();
Is there something like autoConnect(time window)
that aggregates multiple successive subscribers?