以编程方式执行时,Core Data不保存更新。

时间:2012-07-10 12:43:22

标签: objective-c ios core-data restkit

我正在使用CoreData,我想更新我的一个模型中的一个属性,但我无法使用点表示法来使其工作。我的更新操作如下所示:

- (void)addNewsPostToFavourites:(UIButton *)sender
{
    // Returns the right id.
    NSLog(@"Sender tag: %d", sender.tag);

    NSNumber *newsPostId = [NSNumber numberWithInt:sender.tag];
    NewsPost *currentNewsPost = [NewsPost findFirstByAttribute:@"newsPostId" withValue:newsPostId]; 
    // Doesn't seem to do anything.
    currentNewsPost.favourite = @"true";  
}

我也试过[currentNewsPost setValue:@"true" forKey:@"currentNewsPost.favourite"];,但这给了我相同的结果,没有任何反应。

因此我想知道我做错了什么。

还应该提到我在我的项目中也使用RestKit。

任何提示将不胜感激。

1 个答案:

答案 0 :(得分:4)

您必须在更新后保存当前上下文:

[self.context save:nil] // You can put an NSError;