RxJava with Presenter and Saved Slice for Configuration Changes

I am new to RxJava and use this together with MVP architecture.

I found some examples of saving what was observed when changing the configuration using the saved snippet (still not sure if this is the best way to do this). An example I found is the processing of observable data directly from an Activity or Fragment, and not from a presenter.

So, I experimented and set up this quick example (using only Reactivex RxJava and RxAndroid lib) for testing, which seems to work fine. What this example does:

  • Triggers an action with a headless snippet saved.
  • Button
  • The host calls FakeService for a deferred response (5 seconds).
  • The host does .cache () on this observable.
  • The host reports that this save is saved.
  • View saves the observed in the saved fragment.
  • Presenter subscribes to observables.
  • The user performs a configuration change (device rotation). The user can do this as many times as he wants.
  • OnPause tells Presenter CompositeSubscription to clear and unsubscribe from all current subscriptions.
  • Activity is restored and reuses the existing surviving fragment.
  • Activity onResume checks if the saved fragment is stored observable zero.
  • If not null, it tells the subscriber to subscribe to it.
  • The saved observable is signed and, since .cache was called, it simply repeats the result for the new subscriber without calling the service again.
  • When the presenter displays the final result in the view, it also saves the saved fragment with the saved observable null value.

I am wondering if I am doing this correctly, and if there is a more efficient or elegant way to handle configuration changes when the observed subscription is processed in the presenter?


Edit: Thanks for the reply. Based on this, I have achieved what, in my opinion, is a cleaner solution, and I updated the related change example.

With a new change; instead of passing the Observable from Presenter to Activity to the saved Fragment, which should be saved in case of the configurationChange event, I rather set the saved fragment as the second “view” for the Presenter when it was created.

Thus, when onResume () happens after the device rotates, I don’t need to do an Activity so that the ugly plumbing transfers the Observable from the saved fragment back to the Presenter.

The host can simply interact with this second “view” directly and verify the stored observable itself and, if necessary, re-sign. The main activity should no longer be aware of this observable. Suddenly this is a much simpler layer.

+7
android mvp rx-java rx-android
source share
3 answers

Sounds right, good job! Some suggestions:

  • You can simply use Activity.onRetainNonConfigurationInstance() . I heard that it became indefatigable in Android N. You can continue to use the saved fragment if you like it, there is no problem with it, but you do not need it if you prefer not to use the fragments.
  • Why only keep the observed, and not the entire lead? It seems maybe a little wasteful to create a new lead, maybe you can make it work with the same instance that can “attach” and “detach” the view. But again, you need to deal with what to do if your observables emit when you are separated from any species, so maybe that’s good enough.
  • Dan Lew recently made a case in his conversation with Droidcond SF that you should not use cache() . He says replay() gives you more control over what is happening, and replay().autoconnect() works just like cache() does. He convinced me, but see for yourself.
+2
source share

It looks good, you can see this example - https://github.com/krpiotrek/RetainFragmentSample

+3
source share

This https://github.com/MaksTuev/ferro library contains another way to store data in storage and manage background tasks.

the script will look like this

  • Open event, create a presenter
  • Push btn
  • The host calls FakeService for a deferred response (5 seconds).
  • The configuration is changed, the host is not destroyed, Observable is not unsubscrubed, all rx events are frozen
  • Actions created repeatedly, the presenter is reused, the presenter shows previously loaded data for viewing, all rx events are thawed

    I think this help

-one
source share

All Articles