'1. Allows multiple subscribers to call/subscribe to.
'4. Subscribers which subscribe/call the method at a later point just gets the the cached value.
Use replay(1)
in combination with autoConnect()
. This will result in an observable that shares a single subscription to the source, and replays the last value emitted by the source. autoConnect()
ensures that the source is directly subscribed to when the first subscriber subscribes.
- When the processing is done, the subscribers receives the result.
Use Observable.create()
and use the ObservableEmitter
to emit the result.
- The subscribers are automatically unsubscripted to avoid memory leaks. There will be no second event, and no reason to still be subscribed.
Convert the Observable
to a Single
.
Something along the lines of the following should work:
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(final ObservableEmitter<String> e) throws Exception {
Thread.sleep(5000);
e.onNext("Test");
e.onComplete();
}
}).replay(1).autoConnect()
.firstOrError();
Note that you should keep a reference to this Observable
(the result of firstOrError()
) and share that instance with the subscribers.