Learn about IOS App Programming third day
-------State Preservation and restoration
Even though our app supports running in the background, it doesn't always run in the background, and in some cases, the system may need to terminate the app in order to free up memory for apps running in the foreground. But the user does not care whether the app is closed, the user only knows that the app is the place to pause, and when it gets up again it should be in the last place to exit. This allows the user to proceed with the task he did not complete last time. So Uikit implements this function.
Uikit's state-saving system provides a simple and accurate basic technology for saving and recovering app Viewcontroller status. The role of this basic technology is to control the preservation and recovery process at the appropriate time. In order to do that, uikit need developers to this app, development as long as the content of the app to know the line, other systems to help the development done.
The current page save and restore must be supported by the app for both mechanisms, so the app must essentially support Uikit, while allowing Uikit to save and restore the app state at the right time. Because Uikit need to be able to do this, there are more ways to operate.
In both cases, it is necessary to use the app State to save and restore. App status is usually saved when the app comes back to the background. At that time, Uikit need to know that the view and Viewcontrol of your app need not be saved. For each save, the Uikit stores the relevant data to be saved on disk. But when the app starts again, the app will find the last saved data in the disk and restore the data. When recreating the current object, you need to manipulate the code in Viewcontrollerz because the app may need to load the object from the storyboard dynamic load file. Because this loading process is only known by our own code.
The app has a few things to do during the save and restore of app status.
When app state is saved, the app needs to do something like this:
Tell Uikit that the app supports state saving.
Tell those view and Viewcontroller need to be saved.
Encode the corresponding data.
What the app needs to do when the data is restored:
Tell Uikit to support data recovery.
Provide (create) Uikit to recover data required objects.
Decoding requires restoring the object and using it to reach the state before the save.
The task of the app, most of all, is to tell uikit those objects that need to be saved and need to provide those objects in the back up queue. Developers need to spend some time saving and recovering code when designing the state of the app. They also have a lot of control in the current process.
Uikit only save those objects that have a recovery ID, a recovery ID is a unique string that identifies the view and Viewcontrol. The value of this string is very important just to tell Uikit that the object needs to be saved, during the save process, Uikit will need the Viewcontroler level of the app and save all objects with the recovery ID. If a viewcontroller does not have a recovery ID, then all subclasses of this viewcontroler Viewcontroller cannot be saved.
Based on our app, it may not be clear to know how to save every viewcontroller, if Viewcontroller presents a staccato message, we may not want to achieve such an effect, but rather prefer to choose the interactive stability.
For each Viewcontroller developer you can choose to save, and the developer also needs to decide how to recover it, Uikit provides two ways to reconstruct the object, Developers can let us appdelegate recreate one or can assign a restore object to Viewcontroller and let this kind of reconstruction.
Uiviewcontrollerrestoration protocol and to find or create a specified object when recovering, here is a small suggestion:
1 If a viewcontroller is often loaded from the app's main storyboard file, do not assign a recovery class. Let the app agent find the object or use Uikit support to find the object indirectly.
2 for a viewcontroller is not loaded from the Mainstoryboard file, it is assigned to a recovery class. The simplest option is to enable no viewcontroller to have their own recovery class counterpart.
During the save process, the Uikit object will save and record the state of each associated object to disk. Each Viewcontroller object is given another chance to write down records that need to be saved. For example, when a tableview needs to save the Tablecell,uikit that was selected at that time also holds some information such as Viewcontroller's recovery class, and if there is a recovery id,uikit required to save their state information.
When the app comes up again, Uikit loads the app's main storyboard or nib file, usually calling the app's proxy application:willfinishlaunchingwithoptions: method, However, it will revert to the previously saved state. The first thing to do is to ask the app to provide some Viewcontroller objects that were previously saved. If the Viewcontroller class is taken, the page is restored.
To achieve state saving and recovery, you need to do the following:
1must implementapplication:shouldSaveApplicationState:and application: Shouldrestore
ApplicationState: These two proxy methods,
2 requires a non-empty string to be assigned to each Viewcontroller as the recovery ID.
3 If you want to save the state of a particular view, assign a fly-to-empty string to their Restorationidentifier property
4 Assign a recovery class to a suitable Viewcontroller (when the app agent requests viewcontroller on recovery).
5 Viewcontroller serialization and deserialization useencodeRestorableStateWithCoder:和decodeRestorableStateWithCoder:方法。
6 serialization and deserialization of some version information or some status information for the app, recommended Application: willencode-
RestorableStateWithCoder:Andapplication:didDecodeRestorableStateWithCoder:代理方法。
7tableview and CollectionView data source objects should implement the UIDataSourceModelAssociation protocol, although this is not necessary, but this protocol can help to save the corresponding view of the options.