Android-CleanArchitecture icon indicating copy to clipboard operation
Android-CleanArchitecture copied to clipboard

Q: Handling network response during screen rotation

Open youfacepalm opened this issue 8 years ago • 4 comments

Hi. I would like to get your input on how to handle network responses during screen orientation changes. The scenario as follows;

  1. Network request initiated after setView() is called in a Fragment's onViewCreated() method
  2. User rotates the device. onDestroy() gets called which in turn would nullify view interface in Presenter class
  3. Before onCreatedView() method is called again, response arrive from the network request. In this state, the view interface on Presenter would be null and the response is lost.

Thank you

youfacepalm avatar Jan 16 '17 06:01 youfacepalm

@youfacepalm Please check this PR #138 it contains what you looking for.

aftabsikander avatar Feb 18 '17 20:02 aftabsikander

Thank you @aftabsikander for the reply. But, I am not after preserving view states but rather preserving the network response from the request. Think about a user initiating a currency transaction request. I do not want the network request to get lost during orientation changes. I would like to cache the response somehow if the view is in the phase of rotating and deliver when it is visible again.

I could store the response in a presenter variable and deliver when the view is not null. But, I would like to know how would you go about solving this problem in general.

Thanks!

youfacepalm avatar Feb 20 '17 13:02 youfacepalm

@youfacepalm When onDestroy() of Fragment is called it calls destroy() method from the Presenter, this was intended to do the clean up work, if you look at the destroy() method from Presenter it calls dispose() method from specific UseCase class; this is a call to CompositeDisposable which again is disposed in it's destroy() method, when this happens all active Subscriptions are unsubscribed so your data stream is destroyed that is when you have one in the pipeline.

Can you hold a reference to that data, yes you can but not with the existing approach, you will have to use another approach (perhaps cached observables).

arifnadeem7 avatar Mar 02 '17 12:03 arifnadeem7

Check this for few options, page 4. You can retain Presenter, along with returned data from network. Or you have to store Observable returned from network somehow (you can unsubscribe from Subscription!), then resubscribe to that Observable, written here

jemshit avatar Jun 22 '17 07:06 jemshit