I have a CoreData Entity (called “MyData”) that has Attributes that relate to what the user has entered in a variety of UIControls on one of my UIViewControllers, plus one Attribute (“recordKey”) that is a unique identifier for each record. When the user opens the page, I want to check to see if I have a record already for that recordKey. If so, then load it and set all the controls based on what was in that record. If not, set all the controls based off of a “default” record and create a new record for this session, then save the new session when the user exits, but leave the “default” record unchanged. My code (in viewDidLoad) so far is:
MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
NSString *table = @"MyData";
NSError *error;
NSManagedObjectContext *context = [app managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:table inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordKey LIKE %@", self.recordKey];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setPredicate:predicate];
[request setEntity:entity];
NSArray *arr = [context executeFetchRequest:request error:&error];
if (arr.count == 0)
{
// No entries exist for this recordKey, so use default values
predicate = [NSPredicate predicateWithFormat:@"recordKey LIKE %@", @"DEFAULT"];
[request setPredicate:predicate];
NSArray *defaultArr = [context executeFetchRequest:request error:&error];
if (defaultArr.count == 1)
{
MyData *defaultData = [defaultArr objectAtIndex:0]; //create placeholder for for default data
currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context]; //create a new row to correspond to this session
currentData = defaultData; //preload with default values
currentData.recordKey = self.recordKey; //set record key for this session to it's unique identifier
}
else
{
NSLog(@"Error - DEFAULT record not found in table '%@', or multiple DEFAULT records found, in viewDidLoad on RunwayAnalysisController.m",table);
}
}
else if (arr.count == 1)
{
// Record found for this recordKey, so set it as the current MyData object
currentData = [arr objectAtIndex:0];
}
Then when I leave my controller, I just
if (![context save:&error])
{
NSLog(@"Error writing entry to %@ table in saveClick on RunwayAnalysisController.m",table);
}
When I run this, my default records are being overwritten by any changes the user makes during this session. I think the problem is currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context]; currentData = defaultData; isn’t doing what I want it to – create a new entry, load that entry with the information in the default record, then sever the tie with the default record and be on it’s own to be filled as the user interacts with the controls on the page. I’m probably misunderstanding what exactly one of these two lines is doing. Does anyone see what I’m doing wrong here, and suggest how to fix it?
AFAIK you cannot simply fetch a CoreData object and re-insert it into the managedObjectContext. If you want to have a copy, create a new managedObject and set your default values.
In Your ObjectModel you can even set default values to an Entity … this would be an even better way I guess. You then just have to create a new managedObject and modify what needs to be modified. No copying needed. (as long as your default model does not change.)
The way you create a managedObject looks good to me
But then, by setting
currentData = defaultData;you overwrite your previous object. you should do: