Junior — Middle
Explain the differences between Observable objects that immediately start emitting data upon creation and those that start upon subscription in RxJava.
sobes.tech AI
Answer from AI
In RxJava, objects of Observable behave mainly in two ways based on how they emit data:
-
Hot Observable — starts emitting data immediately after creation, regardless of subscribers. Subscribers receive data emitted after they subscribe, but miss data emitted before.
-
Cold Observable — starts emitting only after subscription. Each subscriber gets the full data stream from the beginning.
An example of a hot Observable is an event source like button clicks, which constantly generates events. A cold Observable is reading a file or making a network request, which starts anew for each subscriber.
By default, RxJava's Observable is cold, but it can be made hot using operators like publish() and connect().