kaushikgopal / RxJava-Android-Samples

Learning RxJava for Android by example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Observing a single Observable in multiple fragments/activities

wKovacs64 opened this issue · comments

To observe the same Observable (results of a Retrofit call, for example) in multiple fragments or activities, is it normal to subscribe to the Observable in the same scope where it exists (retained fragment, singleton, whatever) in order to keep the calls from terminating (e.g. chained network calls with flatMap()) and create a new Subscription to the Observable in each interested fragment/activity? Or is there a better approach?

Based on current experience, I do what you suggest as an option i.e. create a new Subscription for each interested fragment/activity. I feel that's more "idiomatic" as it relegates the responsibility of how to react to the event, down to the interested parties.

(Note: your Observable however must be shareable especially for something like a retrofit call, lest creating new Subscriptions everywere would restart same network calls. I'm guessing you already knew that but doesn't hurt to reiterate :))

Cool, thanks for the reassurance.