Austyn Mahoney Austyn Mahoney - 5 months ago 39
Android Question

Replacing EventBus with RxJava - N subscribers always listening

I am replacing an

pattern with
in an Android app. I had events to alert any interested parties of updates to data in my cache singleton. Any time a web service was called, the data would be updated, and the subscribers would be alerted via a posted event.

I have something close to this set up in RxJava with
. The observers get a single event from the subject, but then they get an
event and unsubscribe. This works as the UI first loads, but when the data needs to be refreshed, there are no subscribers to be notified. How do I tell those
to keep listening for more
events from the

I need a
that will report the most recent item.
only emits items after subscription, so it doesn't quite meet my needs. My subscribers start observing at different times (possibly after the first data event), so I need the Subject to emit the last item observed and then keep the stream open for subsequent items. It seems like a combination of AsyncSubject and PublishSubject is what I need. Is there some way to accomplish this with the built in classes, or do I need to create my own subject?

WebServiceObservable OR CacheObservable
/ \
/ \
/ \
UiObserver1 UiObserver2


BehaviorSubject will fit your needs.

If you need more sophisticated behavior you can always write your own Subject implementation. It seems pretty straightforward to do so.