我看到了很多关于 Core Data 更新的问题。实际上我正在创建一个简单的应用程序联系人列表应用程序。它包含添加、编辑、删除和更新功能。这是我的更新代码。它可以工作并更新,但它会更新所有联系人列表。我只需要更新特定的联系人。
- (IBAction)updatePressed:(id)sender
{
delegate = [[AppDelegate alloc]init];
delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
name2 = emailtxt1.text;
email2 = nametext1.text;
mobile2 = numbertxt1.text;
dict = [[NSMutableDictionary alloc] init];
[dict setObject:nametext1.text forKey:@"NAME"];
[dict setObject:emailtxt1.text forKey:@"EMAIL"];
[dict setObject:numbertxt1.text forKey:@"MOBILE"];
[delegate UpdateDiary:dict];
}
- (void)UpdateDiary:(NSMutableDictionary *)dictionary
{
NSLog(@"update book Details Function Entered");
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Diary"inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *mutableFetchResult = [[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy] autorelease];
if (mutableFetchResult == nil) {
NSLog(@"Fetch result error %@, %@", error, [error userInfo]);
}
for (Diary *ob2 in mutableFetchResult)
{
{
ob2.name = [dictionary objectForKey:@"NAME"];
ob2.email=[dictionary objectForKey:@"EMAIL"];
ob2.phone=[dictionary objectForKey:@"MOBILE"];
}
}
if(![self.managedObjectContext save:&error])
{
if([error localizedDescription] != nil)
{
NSLog(@"%@",error);
}
else
{
}
}
}