This repository was archived by the owner on Mar 16, 2021. It is now read-only.
  
  
  
  
persist presenters - fight against process death #53
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Early draft on how to persist Presenters
based on #50
Background
Presenters should be able persist their state so they can reuse their state after the process has been killed. Android provides a simple API (
#onSaveInstanceState()) where Bundles can be saved and restored when a new Activity instance gets created (#onCreate(saveInstanceState)). This is already possible with a few lines of code and no further library APIs are required.The onSaveInstanceState solution has some weaknesses:
onSaveInstanceState()got called. The mutated state never gets saved.Bundleis part of the Android SDK and cannot be used in unit tests (only mocked)Solution
As a solution the Presenter gets a
publicmethod to save its current statepersistState(). The state must be returned byTiPresenter#onSavePersistentState(): byte[].persistState()can be called at any time; whenonSaveInstanceStategets called, later or earlier even when no Activity instance is available.This triggers a
TiPresenterSerializerresponsible for the persistence which can be exchanged with other implementations.Unlike
Activity#onCreate()thepersistanceStatewill not be a parameter ofTiPresenter#onCreate(). This would be a breaking API change and doesn't provide any value compared to a methodgetPersistentState(): byte[]The default implementation of
TiPresenterSerializerwill store the state in a single file per Presenter in<appfolder>/cache/TiPresenterStates/. We cannot detect when a state isn't required anymore, therefore we cannot cleanup those files properly. Some other mechanism must be applied to prevent an always increasing cache folder. I recommend a fixed cache size by deleting the oldest states like OkHttp.TODOs
TiBundleand use it instead ofbyte[]returned bygetPersistentState()for an easy way to save primitives by key and automatically write all into onebyte[]. AndroidsBundlethrowsDeadObjectExceptionwhen created after the Activity is destroyed and cannot be used. May be also a problem for Android State Serializer #51persistentStateat the first launch of a presenterOpen Questions
Contextand cannot be statically initialized likeTiConfiguration.DEFAULTright now. Maybe initialize it in a ContentProvider? Or set the default implementation from the hostingActivity/PresenterInstructorwhen nothing is defined in config?@vRallev @k0shk0sh please review