I'm trying to figure out how to implement state preservation and restoration with Core Data using mmBusinessObjects.
I got as far as adding the following methods in my view controller that used the business object and entity to display my data (besides adding restoration id's to my scenes in my storyboard):
- (void)encodeRestorableStateWithCoder:(NSCoder*)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:data forKey:@"physicalMenuEntity"];
[coder encodeObject:self.physicalMenuBusinessObject forKey:@"physicalMenuBusinessObject"];
}
-(void)decodeRestorableStateWithCoder:(NSCoder*)coder
{
self.physicalMenuBusinessObject = [coder decodeObjectForKey:@"physicalMenuBusinessObject"];
self.physicalMenuEntity = [coder decodeObjectForKey:@"physicalMenuEntity"];
}
but then I got a lot of error messages saying that the selector does not respond to that message. So after doing some reading, I tried making a category to mmBusinessObject so that it would implement NSCoding methods and in mmBusinessObject+Coder.h, I added <NSCoding> to the @interface line and in the m file I wrote this:
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super init])) {
self.dbName = [aDecoder decodeObjectForKey:@"dbName"];
self.entityClassName = [aDecoder decodeObjectForKey:@"entityClassName"];
self.managedObjectContext = [aDecoder decodeObjectForKey:@"managedObjectContext"];
self.copyDatabaseIfNotPresent = [aDecoder decodeObjectForKey:@"copyDatabaseIfNotPresent"];
[aDecoder decodeObjectForKey:@"managedObjectModel"];
[aDecoder decodeObjectForKey:@"persistentStoreCoordinator"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.dbName forKey:@"dbName"];
[aCoder encodeObject:self.entityClassName forKey:@"entityClassName"];
[aCoder encodeObject:self.managedObjectContext forKey:@"managedObjectContext"];
[aCoder encodeBool:self.copyDatabaseIfNotPresent forKey:@"copyDatabaseIfNotPresent"];
[aCoder encodeObject:self.managedObjectModel forKey:@"managedObjectModel"];
[aCoder encodeObject:self.persistentStoreCoordinator forKey:@"persistentStoreCoordinator"];
}
Anyway, I tried testing it by launching my app in my iPad through Xcode 6, navigating to the point where my Core Data objects were being used, placed my app in the background, stopped Xcode from running then attempted to relaunch my app in the iPad. Basically the app just quits immediately as soon as I try to do so, so I know that my implementation of the category is either incomplete or just simply on the wrong track. Any suggestions on how to make state preservation and restoration work with mmBuisnessObject and Core Data?