1

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?

4

1 回答 1

1

AFAIK,您不能简单地获取 CoreData 对象并将其重新插入 managedObjectContext。如果您想要一个副本,请创建一个新的 managedObject 并设置您的默认值。

在您的 ObjectModel 中,您甚至可以为实体设置默认值……我猜这将是一种更好的方式。然后,您只需创建一个新的 managedObject 并修改需要修改的内容。无需复制。(只要您的默认模型没有改变。)

您创建 managedObject 的方式对我来说看起来不错

MyData *defaultData = [defaultArr objectAtIndex:0];
currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context];

但是,通过设置currentData = defaultData;你覆盖你以前的对象。你应该做:

currentData.var1 = defaultData.var1;
currentData.var2 = defaultData.var2;
...
于 2012-11-06T14:40:37.423 回答