The Controller
+ EventBus
pattern works very well in Android, but firing events everywhere gets really messy. If a controller gets asks to load the same data multiple times, it's easy to have it start loading the first time, and ignore subsequent requests while its loading, and start listening to future loading requests once it's done loading. How can I do this with RxJava (using MVP presenters?)
Scenario
I have a singleton ColorModel
in my android app that loads my favorite color (from multiple sources), as an Observable<String>
(String == hexcode).
My ColorPresenter
hooks into the ColorModel
, converting the String
hexcode into a Color
object that can be displayed in the ColorView
.
These are the main pieces of my logic:
Observable<String> getColor()
(fromColorModel
)Observable<Color> getColorForView()
(fromColorPresenter
)subscribe()
(inColorView
)
Problems
- #1 is created on every subscription. If I have multiple presenters calling #1, this is very wasteful. I don't want this step repeated (only initiated on the first call, then cached after).
- When I unsubscribe during
onStop()
, #2 gets cancelled (desired), but that unsubscription bubbles up to #1 being cancelled too (not desired).