Core data NSMangeObject通知和保存:
我使用核心数据来持久化我的数据模型,这非常简单,三个实体如下:A>B>C。当我的模型发生变化时,我需要执行一些活动。为此,我正在使用viewDidLoad中的以下代码侦听NSManagedObjectContextObjectsIDChangeNotification:Core data NSMangeObject通知和保存:,core-data,nsmanagedobjectcontext,nsnotification,Core Data,Nsmanagedobjectcontext,Nsnotification,我使用核心数据来持久化我的数据模型,这非常简单,三个实体如下:A>B>C。当我的模型发生变化时,我需要执行一些活动。为此,我正在使用viewDidLoad中的以下代码侦听NSManagedObjectContextObjectsIDChangeNotification: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updat
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateReminder:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:self.managedObjectContext];
在方法updateReminder中,我做了需要做的事情。
问题在于,当通知执行时,更改不会持久化。如果创建了新实体但未保存,如果更改了某些内容,则不会保存更改。
有什么想法吗
编辑:
updateReminder的代码如下:
- (void)updateReminder:(NSNotification *)notification
{
A *entityAUpdated = [[notification userInfo] objectForKey:NSUpdatedObjectsKey];
if (entityAUpdated) {
...Do something when entityA is updated.
}
A *entityADeleted = [[notification userInfo] objectForKey:NSDeletedObjectsKey];
if (entityADeleted) {
...Do something when entityA is Deleted.
}
A *entityAInserted = [[notification userInfo] objectForKey:NSInsertedObjectsKey];
if (entityAInserted) {
...Do something when entityA is Inserted
}
}
您是否在任何地方调用
save:
?它不在您发布的代码中。是的,在创建实体时调用save:。更改仅在NSManagedObjectContextDidSaveNotification
(保存后)保存。在这里,您正在观察便笺簿上下文的更改(nsmanagedObjectContextObjectsIDChangeNotification
)是的,我在观察,什么也不做。为什么managedObjectContext不能继续正常执行?当您说数据没有被保存时,您的意思是它永远不会出现?如果退出应用程序并重新启动,新数据是否不存在?