Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS State Preservation and Restoration

Rob Brown
December 13, 2012

iOS State Preservation and Restoration

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.

Rob Brown

December 13, 2012
Tweet

More Decks by Rob Brown

Other Decks in Programming

Transcript

  1. 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
  2. NSCoding Similar to writing to a plist Any object that

    conforms to NSCoding can be encoded
  3. plist NSCoding NSNumber Y Y NSString Y Y NSDictionary Y

    Y NSArray Y Y NSData Y Y NSDate Y Y NSSet N Y Blocks N N Anything Else N Y
  4. NSSecureCoding Irrelevant to state restoration, but nice to know Securely

    persists data Many Cocoa classes conform to NSSecureCoding
  5. 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
  6. Preservation Flow - (void)encodeRestorableStateWithCoder:(NSCoder *)coder { [super encodeRestorableStateWithCoder:coder]; // Assumes

    Person is NSCoding-compliant [coder encodeObject:self.person forKey:@“person”]; }
  7. 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]; }
  8. 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
  9. UIDataSourceModelAssociation - (NSString*)modelIdentifierForElementAtIndexPath:(NSIndexPath *)idx inView:(UIView *)view { // Uses Core

    Data Person * person = self.persons[idx.row]; return person.objectID.URIRepresentation.absoluteString; }
  10. 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]; }
  11. Must use proper UIViewController containment Restoration identifier paths must be

    unique For a view controller to be restored, it must have a path to the root view controller Manually terminating the app will delete the state Gotchas
  12. Gotchas Restore only minimal state in +viewControllerWithRestorationIdentifierPath: If a view

    controller that was persisted is not restored, the app delegate is given a chance to restore it Testing state restoration isn’t straightforward Press home button, then have Xcode kill the app
  13. Gotchas If you change your view controller hierarchy, make sure

    you don’t restore from an older state If your app crashes during restoration, the state data is thrown out
  14. Want to Learn More? WWDC Session 208 Archives and Serializations

    Programming Guide iOS App Programming Guide