There could be complex scenarios wherein we should be able to automatically unsubscribe from an Observable to subscribe to a new one in a continuous subscribe-unsubscribe sequence.
RxJava's switch()
, as per the definition, transforms an Observable emitting Observables into an Observable emitting the most recent emitted Observable.
Given a source Observable that emits a sequence of Observables, switch()
subscribes to the source Observable and starts emitting the same items emitted by the first emitted Observable. When the source emits a new Observable, switch()
immediately unsubscribes from the old Observable (thus interrupting the item flow from it) and subscribes to the new one, starting to emit its items.
13.59.141.75