如何更新核心数据中任何键对应的数据?
由于
答案 0 :(得分:1)
要更新核心数据中的任何条目,您需要先获取该条目,然后为该实体创建对象,然后设置其所有属性,请参阅此代码,
NSMutableArray *fetchResults;
NSString *entityName=@"YourEntity";
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:globalManagedObjectContext];
[fetchRequest setEntity:entity];
fetchResults = [NSMutableArray arrayWithArray:[globalManagedObjectContext executeFetchRequest:fetchRequest error:nil]];
[fetchRequest release];
[fetchResults filterUsingPredicate:[NSPredicate predicateWithFormat:@"id==%@",comparing_key]];
objEntity=[fetchResults objectAtIndex:0];
objEntity.pro1=@"some value";
objEntity.pro2=@"some value";
if (![globalManagedObjectContext save:nil])
NSLog(@"Error in storing to database");