小编Zen*_*ula的帖子

将NSManagedObject的实例添加到NSManagedObjectContext ok,更新同一实例失败

我在我的iPhone应用程序中使用核心数据.我创建了一个简单的类Friend,它派生自NSManagedObject并使用以下属性:

@property (nonatomic, retain) NSString *name;
Run Code Online (Sandbox Code Playgroud)

我能够在我的上下文中添加和删除此类的实例,并且我的更改也是持久的.
现在我想更新/修改一个Friend -instance并让它再次持久化.

但这似乎不起作用.

这是一段显示我的问题的代码:

//  NSManagedObjectContext *context  = < my managed context> 
//   NSFetchedResultsController *nsfrc= < my fetched result controller>

NSEntityDescription *entity = [nsfrc entity];
NSManagedObject *newManagedObject = [NSEntityDescription 
       insertNewObjectForEntityForName:[entity name]  inManagedObjectContext:context];

Friend *f = (Friend *) newManagedObject;
f.name = @"name1";
//1.  --- here context.hasChanges == 1 ---   ok

NSError *error = nil;
if (![context save:&error]) { ... }
//2.  --- here context.hasChanges == 0 --- …
Run Code Online (Sandbox Code Playgroud)

iphone core-data nsmanagedobject nsmanagedobjectcontext

4
推荐指数
1
解决办法
3668
查看次数