• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
iOS State Preservation and Restoration
 

iOS State Preservation and Restoration

on

  • 4,108 views

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 ...

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.

Statistics

Views

Total Views
4,108
Views on SlideShare
4,106
Embed Views
2

Actions

Likes
1
Downloads
20
Comments
0

1 Embed 2

http://www.slashdocs.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    iOS State Preservation and Restoration iOS State Preservation and Restoration Presentation Transcript

    • iOS State Preservation andRestorationRobert BrownTwitter: @robby_brownapp.net: @robert_brown
    • 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
    • NSCodingSimilar to writing to a plistAny object that conforms to NSCoding can beencoded
    • 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
    • NSCoding-initWithCoder: -decodeObjectForKey:-encodeWithCoder: -encodeObject:forKey:
    • NSSecureCodingIrrelevant to state restoration, but nice to knowSecurely persists dataMany Cocoa classes conform to NSSecureCoding
    • 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
    • Restoration Identifiers /A /A/B /A/B /A/C /A/C/B
    • Preservation Flow-application:shouldSaveApplicationState: 1-application:willEncodeRestorableStateWithCoder: 2 3 4 Each window on main screen Save view controller Save view 5
    • Preservation Flow - (void)encodeRestorableStateWithCoder:(NSCoder *)coder { [super encodeRestorableStateWithCoder:coder]; // Assumes Person is NSCoding-compliant [coder encodeObject:self.person forKey:@“person”]; }
    • Restoration Flow -application:willFinishLaunchingWithOptions: -application:shouldRestoreApplicationState: Yes State Restoration No -application:didDecodeRestorableState:withCoder: -application:didFinishLaunchingWithOptions:
    • Restoration Flow 1 +viewControllerWithRestorationIdentifierPath:coder:Each view controller 2 3 5 Restore view controller stateFound view controllers Restore view 4 -decodeRestorableStateWithCoder:
    • 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];}
    • Restoration Flow- (void)decodeRestorableStateWithCoder:(NSCoder*)coder { [super decodeRestorableStateWithCoder:coder]; self.person = [coder decodeObjectForKey:@“person”];}
    • 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
    • UIDataSourceModelAssociation -modelIdentifierForElementAtIndexPath:inView: Converts from model object to ID -indexPathForElementWithModelIdentifier:inView: Converts from ID to model object
    • UIDataSourceModelAssociation - (NSString*)modelIdentifierForElementAtIndexPath:(NSIndexPath *)idx inView:(UIView *)view { // Uses Core Data Person * person = self.persons[idx.row]; return person.objectID.URIRepresentation.absoluteString; }
    • 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];}
    • Demo
    • 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
    • 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
    • GotchasRestoration classes must conform to theUIViewControllerRestoration protocolRestoration classes aren’t necessary when usingUIStoryboard
    • 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
    • Want to Learn More?WWDC Session 208Archives and Serializations Programming GuideiOS App Programming Guide
    • Questions?