iOS State Preservation and Restoration


Published on

Covers iOS app state preservation and restoration with extra emphasis on NSCoding. WWCD 2012 session 208 glosses over some of the fine details of how NSCoding works. Understanding this foundation better helps in understanding state preservation and restoration.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

iOS State Preservation and Restoration

  1. 1. iOS State Preservation andRestorationRobert BrownTwitter: @robert_brown
  2. 2. What is State Restoration? Restores your app where the user left it Makes your apps appear as if they never terminated Lets users seamlessly get back to what they want
  3. 3. NSCodingSimilar to writing to a plistAny object that conforms to NSCoding can beencoded
  4. 4. plist NSCoding NSNumber Y Y NSString Y YNSDictionary Y Y NSArray Y Y NSData Y Y NSDate Y Y NSSet N Y Blocks N NAnything Else N Y
  5. 5. NSCoding-initWithCoder: -decodeObjectForKey:-encodeWithCoder: -encodeObject:forKey:
  6. 6. NSSecureCodingIrrelevant to state restoration, but nice to knowSecurely persists dataMany Cocoa classes conform to NSSecureCoding
  7. 7. Restoration Identifiers Each view controller you want to restore must define a restoration identifier Restoration identifiers make up a restoration ID path Restoration paths must be unique Example: /TabBarController/AboutViewController
  8. 8. Restoration Identifiers /A /A/B /A/B /A/C /A/C/B
  9. 9. Preservation Flow-application:shouldSaveApplicationState: 1-application:willEncodeRestorableStateWithCoder: 2 3 4 Each window on main screen Save view controller Save view 5
  10. 10. Preservation Flow - (void)encodeRestorableStateWithCoder:(NSCoder *)coder { [super encodeRestorableStateWithCoder:coder]; // Assumes Person is NSCoding-compliant [coder encodeObject:self.person forKey:@“person”]; }
  11. 11. Restoration Flow -application:willFinishLaunchingWithOptions: -application:shouldRestoreApplicationState: Yes State Restoration No -application:didDecodeRestorableState:withCoder: -application:didFinishLaunchingWithOptions:
  12. 12. Restoration Flow 1 +viewControllerWithRestorationIdentifierPath:coder:Each view controller 2 3 5 Restore view controller stateFound view controllers Restore view 4 -decodeRestorableStateWithCoder:
  13. 13. Restoration Flow+viewControllerWithRestorationIdentifierPath:(NSArray *)components coder:(NSCoder *)coder { // Restore only what you absolutely need // You may not be able to restore other view controllers return [self new];}
  14. 14. Restoration Flow- (void)decodeRestorableStateWithCoder:(NSCoder*)coder { [super decodeRestorableStateWithCoder:coder]; self.person = [coder decodeObjectForKey:@“person”];}
  15. 15. UIDataSourceModelAssociation Many views will automatically restore their state UITableView and UICollectionView need some extra help to restore your visible and selected cells UIDataSourceModel association provides a mapping of cells to model objects This is NOT intended to be used to persist data
  16. 16. UIDataSourceModelAssociation -modelIdentifierForElementAtIndexPath:inView: Converts from model object to ID -indexPathForElementWithModelIdentifier:inView: Converts from ID to model object
  17. 17. UIDataSourceModelAssociation - (NSString*)modelIdentifierForElementAtIndexPath:(NSIndexPath *)idx inView:(UIView *)view { // Uses Core Data Person * person = self.persons[idx.row]; return person.objectID.URIRepresentation.absoluteString; }
  18. 18. UIDataSourceModelAssociation- (NSIndexPath *)indexPathForElementWithModelIdentifier:(NSString *)identifier inView:(UIView *)view { NSURL * uri = [NSURL urlWithString:identifier]; NSPersistentStoreCoordinator * coordinator = // Your coordinator NSManagedObjectContext * context = // Your context NSManagedObjectID * objectID = [coordinator managedObjectIDForURIRepresentation:uri]; Person * person = [context existingObjectWithID:objectID error:NULL]; NSInteger index = [self.persons indexOfObject:person]; return [NSIndexPath indexPathForRow:index inSection:0];}
  19. 19. Demo
  20. 20. GotchasMust use proper UIViewController containmentRestoration identifier paths must be uniqueFor a view controller to be restored, it must have a pathto the root view controllerManually terminating the app will delete the state
  21. 21. GotchasRestore only minimal state in+viewControllerWithRestorationIdentifierPath:If a view controller that was persisted is not restored,the app delegate is given a chance to restore itTesting state restoration isn’t straightforward Press home button, then have Xcode kill the app
  22. 22. GotchasRestoration classes must conform to theUIViewControllerRestoration protocolRestoration classes aren’t necessary when usingUIStoryboard
  23. 23. GotchasIf you change your view controller hierarchy, make sureyou don’t restore from an older stateIf your app crashes during restoration, the state data isthrown out
  24. 24. Want to Learn More?WWDC Session 208Archives and Serializations Programming GuideiOS App Programming Guide
  25. 25. Questions?