Historical Restoration Contractors Lenexa Kansas: Historic Preservation Consultants
iOS State Preservation and Restoration
-
Upload
robert-brown -
Category
Technology
-
view
5.044 -
download
0
description
Transcript of iOS State Preservation and Restoration
![Page 1: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/1.jpg)
iOS State Preservation and RestorationRobert BrownTwitter: @robby_brownapp.net: @robert_brown
![Page 2: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/2.jpg)
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
![Page 3: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/3.jpg)
NSCoding
Similar to writing to a plist
Any object that conforms to NSCoding can be encoded
![Page 4: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/4.jpg)
plist NSCodingNSNumber Y YNSString Y Y
NSDictionary Y YNSArray Y YNSData Y YNSDate Y YNSSet N YBlocks N N
Anything Else N Y
![Page 5: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/5.jpg)
NSCoding
-initWithCoder:
-decodeObjectForKey:
-encodeWithCoder:
-encodeObject:forKey:
![Page 6: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/6.jpg)
NSSecureCoding
Irrelevant to state restoration, but nice to know
Securely persists data
Many Cocoa classes conform to NSSecureCoding
![Page 7: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/7.jpg)
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
![Page 8: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/8.jpg)
Restoration Identifiers/A
/A/C/A/B/A/B
/A/C/B
![Page 9: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/9.jpg)
Preservation Flow
-application:shouldSaveApplicationState:
-application:willEncodeRestorableStateWithCoder:
Each window on main screen Save view controller Save view
1
2
3 4
5
![Page 10: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/10.jpg)
Preservation Flow
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
// Assumes Person is NSCoding-compliant
[coder encodeObject:self.person forKey:@“person”];
}
![Page 11: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/11.jpg)
Restoration Flow-application:willFinishLaunchingWithOptions:
-application:shouldRestoreApplicationState:
-application:didFinishLaunchingWithOptions:
State Restoration
Yes
No
-application:didDecodeRestorableState:withCoder:
![Page 12: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/12.jpg)
Restoration Flow
Each view controller +viewControllerWithRestorationIdentifierPath:coder:
Found view controllers
-decodeRestorableStateWithCoder:
Restore view controller state Restore view
1
3
4
5
2
![Page 13: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/13.jpg)
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];
}
![Page 14: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/14.jpg)
Restoration Flow
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
self.person = [coder decodeObjectForKey:@“person”];
}
![Page 15: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/15.jpg)
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
![Page 16: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/16.jpg)
UIDataSourceModelAssociation
-modelIdentifierForElementAtIndexPath:inView:
Converts from model object to ID
-indexPathForElementWithModelIdentifier:inView:
Converts from ID to model object
![Page 17: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/17.jpg)
UIDataSourceModelAssociation
- (NSString*)modelIdentifierForElementAtIndexPath:(NSIndexPath *)idx inView:(UIView *)view {
// Uses Core Data
Person * person = self.persons[idx.row];
return person.objectID.URIRepresentation.absoluteString;
}
![Page 18: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/18.jpg)
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];
}
![Page 19: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/19.jpg)
Demo
![Page 20: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/20.jpg)
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
![Page 21: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/21.jpg)
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
![Page 22: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/22.jpg)
Gotchas
Restoration classes must conform to the UIViewControllerRestoration protocol
Restoration classes aren’t necessary when using UIStoryboard
![Page 23: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/23.jpg)
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
![Page 24: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/24.jpg)
Want to Learn More?
WWDC Session 208
Archives and Serializations Programming Guide
iOS App Programming Guide
![Page 25: iOS State Preservation and Restoration](https://reader033.fdocuments.net/reader033/viewer/2022051609/547e21edb37959652b8b53ec/html5/thumbnails/25.jpg)
Questions?