核心数据'保存'方法创建重复的对象

时间:2016-07-15 09:42:26

标签: ios objective-c core-data nsmanagedobjectcontext

我试图解决这个问题,因为昨天没有成功,直到现在。假设我在核心数据中有5个对象,每个对象有两个字段,例如first_name和last_name,我有一个方法,它接受一个唯一标识每个对象的参数。现在在我的方法中我改变了一个对象的一些属性,让我们假设我改变了一个对象的first_name。这是进行更改后的代码,以便将这些更改保存到核心数据中。

NSError *saveToContextErr;
if (![appdelegate.managedObjectContext save:&saveToContextErr])
{
    [self removeLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"The app could not save data to device.Please contact A2OMobile Support."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

然而,当执行此操作时,我的tableview中有重复的条目,显示所有对象。所以基本上旧的对象仍然存在,不应该发生。有人可以指出我要去的地方。任何帮助表示赞赏。 在核心数据中添加对象的代码:

-(void)saveToContext :(NSString *)aGuid
{    
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(!editing)
    contact = (A2OContact *)[NSEntityDescription insertNewObjectForEntityForName:@"A2OContact" inManagedObjectContext:appdelegate.managedObjectContext];
contact.fname = fnameTextField.text;

接下来是上面的代码以保存更改。

0 个答案:

没有答案