CoreData NSManagedObject未更新

时间:2013-10-10 21:31:08

标签: ios objective-c core-data updates

我正在尝试更新Core Data堆栈中的现有对象, 正在检索该对象,我可以更改该值,但在保存上下文时,不会保存任何内容。 hasChanges为NO,updatedObjects为空。

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: @"MyModel"];
NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"(x == %@) AND (y == %@)", x, y];

request.returnsObjectsAsFaults = NO;
request.predicate       = predicate;

NSArray *results        = [self.managedObjectContext executeFetchRequest: request error: nil];
MyModel *model          = results[0];

model.newvalue          = "somenewvalue";

NSLog(@"%@", (self.managedObjectContext.hasChanges)?@"YES":@"NO"); // -> Shows NO
NSLog(@"%@", self.managedObjectContext.updatedObjects); // -> Shows empty array

[self saveContext]; // No error, seems to be working successfully but the updated objects is empty as well has "hasChanges", so something else is being missed here

这有点令人困惑和恼人。我真的很感激有关这个问题的任何见解。

正如所建议的那样,也尝试了但没有帮助

NSManagedObject *model  = results[0];
[model setValue: @"somenewvalue" forKey: @"newvalue"];

1 个答案:

答案 0 :(得分:0)

疯狂的回答,但我绝对可以想象别人犯了这个错误。 在我的NSManagedObject内,而不是 @dynamic ,我有 @synthesize 。那就是问题。