Friday, 22 May 2015

Preserve and Restore the state of the App

State Preserving and Restoration Process:
Application state preservation and restoration is all about presenting the user with application continuity in terms of appearance and behavior. The UIKit preservation and restoration system provides a mechanism by which an application is able to save and restore the state of specific view controllers and views between different application invocations. UIKit achieves this by defining a flexible structure to which the application must conform in terms of providing information on what is to be saved, and implementing methods that are called by UIKit at certain points during the preservation and restoration process.

During the application design process, the developer must decide which view controllers and views that comprise the application need to have state preserved to ensure continuity for the user. Each item for which state is to be saved must then be assigned a restoration identifier. 

Note:If a view controller does not have a restoration ID, none of the controller’s child views or view controllers will be saved, irrespective of whether or not those sub-views have a restoration ID.

Preserve the State:
Each time a running application is placed into the background, UIKit will ask the application whether or not it requires state preservation. In the event that the application requires the state to be saved, UIKit will traverse the view controller hierarchy of the application and save the state of each object that has a restoration ID. As it does this, it will call a method on each eligible object in order to provide that object with an opportunity to encode and return additional data to be included in the saved state. Once the state information has been gathered, it is saved to a file on the local file system of the device.

Restore the State:
When the application is next launched (as opposed to being brought out of the background and into the foreground) UIKit will look for a saved state file for the application. In the event that it finds one, it will ask the application if state restoration is required. If the application responds affirmatively, UIKit will use the saved state to guide the application through the process of re-creating the views and view controllers to the previous state.


The application preservation and restoration is controlled by a series of interactions between UIKit, the application Delegate and preserved viewControllers and views as summarized in flow chart;

State Restoration



Example: 
1) In order to implement state preservation and restoration  ,implement the  methods in the application delegate which return a boolean value to indicate whether or not preservation and restoration are required.

-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{
    return YES;
}

-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{
    return YES;

}

2) When UIKit walks the view controller hierarchy of an application to preserve state, only those objects with a restoration ID will be saved.
Restoration IDs can be assigned to objects either in code or from within Interface Builder. The restoration ID can be any valid string and may be assigned in code via the restorationID property of the UIView and UIViewController classes. For example:
myViewController.restorationIdentifier =@"thirdViewController";

3) By default, the following state information is saved and restored automatically for view controllers:
     Currently presented view controller
     Currently selected tab
     State of navigation stacks

In the case of views, the following is preserved by default:
   Current scroll position
   Currently selected cell in a table view
   Current state of an image view (zoom, pan, etc)
   Web history (including scroll position and zoom level)


Saving Additional Information:
4) Some times in we want to save the additional data with object to be saved while preserving the state of the app and restoring.
When the state of a specific view controller is to be saved, it will check to see if a method named encodeRestorableStateWithCoder has been implemented in that object’s class. If the method has been implemented, UIKit will call that method, passing through a reference to an NSCoder object. It is then the responsibility of that method to store any additional state data that needs to be preserved into that NSCoder object before returning. UIKit will then save that NSCoder object along with the rest of the application’s state.

When UIKit restores the view controller object on a subsequent launch of the application, it will call the decodeRestorableStateWithCoder method of that object, passing through the NSCoder object containing the previously stored state data. The method is then responsible for decoding the object and using the data contained therein to restore the view to the previous state.
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder{
    [coder encodeObject:self.txtView.text forKey:@"unsaveText"];
    
    [coder encodeObject:UIImagePNGRepresentation(self.imgView.image)
                 forKey:@"YourImageKey"];
    [super encodeRestorableStateWithCoder:coder];
}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder{
   self.txtView.text = [coder decodeObjectForKey:@"unsaveText"];
    self.imgView.image = [UIImage imageWithData:[coder decodeObjectForKey:@"YourImageKey"]];
    [super decodeRestorableStateWithCoder:coder];
}

5)Saving General Application State:
There will also be situations where other data may be relevant to the state of the application but not directly associated with the user interface elements. In order to address this need, the following two methods may be implemented within the application delegate class:
  • application:willEncodeRestorableStateWithCoder
  • application:didEncodeRestorableStateWithCoder
The former method is called by UIKit at the start of the preservation process and is passed a reference to an NSCoder object into which state data may be stored. The application:didEncodeRestorableStateWithCoder method, on the other hand, is called when UIKit has completed the restoration process and is passed the NSCoder object into which general state data was previously stored.


References:
http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html
http://www.techotopia.com/index.php/An_Overview_of_iOS_7_Application_State_Preservation_and_Restoration

3 comments: