In this chapter, we covered multicasting using ConnectableObservable
and Subject
. The biggest takeaway is that Observable
operators result in separate streams of events for each Observer
that subscribes. If you want to consolidate these multiple streams into a single stream to prevent redundant work, the best way is to call publish()
on an Observable
to yield ConnectableObservable
. You can then manually call connect()
to fire emissions once your Observers are set up or automatically trigger a connection using autoConnect()
or refCount()
.
Mutlicasting also enables replaying and caching, so tardy Observers can receive missed emissions. Subjects provide a means to multicast and cache emissions as well, but you should only utilize them if existing operators cannot achieve what you want.
In the next chapter, we will start working with concurrency. This is where RxJava truly shines and is often the selling point of reactive programming.