Author Topic: State Preservation and Restoration with mmBusinessObject  (Read 9218 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
State Preservation and Restoration with mmBusinessObject
« on: October 05, 2014, 07:49:26 PM »
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):

Code: [Select]
- (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:

Code: [Select]

-(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?
« Last Edit: October 05, 2014, 09:01:24 PM by rtamesis »

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: State Preservation and Restoration with mmBusinessObject
« Reply #1 on: October 06, 2014, 01:47:08 PM »
What state information are you trying to save on the business object?

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: State Preservation and Restoration with mmBusinessObject
« Reply #2 on: October 06, 2014, 03:49:39 PM »
I'm using Core Data to manage a database containing questions and answers for a quiz app, and my view controller has to display the questions and check the student's answer. I can save and restore the text of the question in the current page, but other than that, I can't display anything else like the current score or jump into the next question, so I thought I needed to preserve both the business object and the entity associated with it to make it work, but so far no luck. After reading what others have posted online about Core Data and state preservation I'm at the point where I think that this is simply too much trouble to be worth implementing.

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: State Preservation and Restoration with mmBusinessObject
« Reply #3 on: October 06, 2014, 04:35:49 PM »
While trying to implement state preservation may be a good thing and trying various solutions, I've finally decided that the work involved with Core Data objects is not trivial at all, is beyond my current skill set and will consume too much time trying to make it work, so I'm going to abandon that effort. Sorry for bringing this question up.

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: State Preservation and Restoration with mmBusinessObject
« Reply #4 on: October 07, 2014, 04:32:09 PM »
If you need to persist state when the app closes and opens up, you can simply create an entity that contains the information you want to persist, and save that via core data. There's no need to save mmBusinessObject...you can simply use the business object to retrieve and store the entity.

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: State Preservation and Restoration with mmBusinessObject
« Reply #5 on: October 10, 2014, 08:37:30 PM »
Thanks for the suggestion. I also think that will make an excellent tutorial for your next book!