我在更新核心数据中的特定记录时遇到了麻烦。在我的代码中发生了什么,它确实改变了值,但是当我重新运行应用程序时,它会恢复到原始值。为什么会这样?
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Wish" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemWish = %@", self.wishItemStr];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
[fetchedObjects setValue:@"YES" forKeyPath:@"isAchieved"];
if (fetchedObjects == nil) {
NSLog(@"no fetched objects!");
}
答案 0 :(得分:0)
您需要save
上下文。
[managedObjectContext save:&error];