Search code examples
reactivex

Many observer connected to one observable receive events one at a time


This is how I create my observable:

Observable.fromCallable(new EventObtainer()).flatMap(Observable::from).subscribeOn(Schedulers.io()).repeat();

And after that, through http request i'm trying to add different observers. The thing is that if I have more than one observer I can't predict which observer will obtain emitted item. Why doesn't observable emit item to every subscribed observer, but one item at time to different observers?


Solution

  • I resolved this,

    In observable contract: http://reactivex.io/documentation/contract.html There is information:

    There is no general guarantee that two observers of the same Observable will see the same sequence of items.

    So i resolved it by making my observable Connectable observable by publish, and then invoke connect method on it:

    Observable.fromCallable(new EventObtainer()).flatMap(Observable::from).subscribeOn(Schedulers.io()).repeat().publish();
    observable.connect();
    

    and now even if asynchronously i will add more observers it will emit obtained item to every observers.