如何更新提取的托管对象?

时间:2014-06-01 12:07:34

标签: ios objective-c

我在更新核心数据中的特定记录时遇到了麻烦。在我的代码中发生了什么,它确实改变了值,但是当我重新运行应用程序时,它会恢复到原始值。为什么会这样?

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!");
}

1 个答案:

答案 0 :(得分:0)

您需要save上下文。

[managedObjectContext save:&error];

我建议你阅读these very good tutorials and articles on objc.io