如何观察NSManagedObject是否从managedObjectContext中删除 [英] How to observe if a NSManagedObject is removed from managedObjectContext

查看:70
本文介绍了如何观察NSManagedObject是否从managedObjectContext中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的某一点(和加载另一个存储)和 reset 我的managedObjectContext从persistentStoreCoordinator中删除persistentStore。
当我这样做,根据文档,我还需要删除所有被提取的managedObjects的引用:

I am removing the persistentStore from a persistentStoreCoordinator at some point in my application (and loading another store) and reset my managedObjectContext. When I do that, according to the documentation, I also need to delete all references to managedObjects that have been fetched:


所有接收者的管理对象都被忘记了。如果你使用这个
方法,你应该
确保你也丢弃对使用接收者抓取的任何管理对象的引用,

,因为他们

All the receiver's managed objects are "forgotten." If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver,
since they will be invalid afterwards.

我想避免浏览所有的fetchedResultsControllers,缓存,可能包含managedObjects的数组视图也存储对象等。

I would like to avoid having to go through all my fetchedResultsControllers, caches, arrays that may contain managedObjects, detail views that also store an object, etc.

而是我宁愿观察被管理对象的 isInserted 状态变化。像

Instead I'd prefer to observe if the managed object's isInserted status changes. Something like

[myObject addObserver:self
           forKeyPath:@"isInserted"
              options:0
              context:nil];

不幸的是,这似乎不起作用。

Unfortunately this doesn't seem to work.

那么,如何观察NSManagedObject是否从managedObjectContext中删除?

So – how can I observe if a NSManagedObject is removed from managedObjectContext?

推荐答案

我发现观察 NSPersistentStoreCoordinatorStoresDidChangeNotification 在我的情况下工作得很好。
它被调用两次 - 第一次,当旧的持久存储被删除时,第二次添加新存储时。

I have found that observing NSPersistentStoreCoordinatorStoresDidChangeNotification works pretty well in my case. It is being called twice – the first time, when the old persistent store is removed, the second time when the new store is added.

我仍在测试这个解决方案比我的情况更好地证明尼古拉斯的建议。

I am still testing wether this solution proofs better for my case than the one suggested by Nicholas.

这篇关于如何观察NSManagedObject是否从managedObjectContext中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆