Sometimes, it is necessary to have an object that can subscribe to an Observable while being able to act as an Observable at the same time. It is useful to have a way to glue different Observables in an independent manner.
A Subject is a way to do this in RxJava and, in this chapter, we will explore various types of Subjects and on what occasions they should be used.
In RxJava, there are four different classes of Subject:
- ReplaySubject
- AsyncSubject
- BehaviorSubject
- PublishSubject
Each of them has different use cases, and we will cover their unique aspects so that the developer will be able to make the correct choice.
Furthermore, in this chapter, we will see how we can use Subject for preferences (settings) management. We will combine Subjects along with the RxPreferences library to persist settings between the different runs of the application.
We will learn that Subjects can be naturally integrated into the existing financial stock quote retrieval flow and how it will keep the app reactive to the changes that the user will make.
Also, we will provide a simple User Interface to interact with the settings for the user.
To summarize, we will cover the following topics:
- What are Subjects
- What kind of Subjects are available and how do they differ
- How can RxPreferences be used to store settings
- How to integrate RxPreferences and Subjects into the main flow to achieve a reactive handling of the preferences updates
Finally, in the next chapter, which is the last one, we will cover RxMarble diagrams, which are very often found in the documentation and often help quite a lot to understand operators.