我有一个多上下文核心数据应用程序,它大量使用观察器来保持 UI 的状态。我遇到了一系列删除问题,这些问题是因为删除了观察到的对象。我很乐意用“if ([mo isDeleted]) return;”来保护我的观察者:
From NSManagedObject documentation
The method returns YES if Core Data will ask the persistent store to delete the object during the next save operation. It may return NO at other times, particularly after the object has been deleted. The immediacy with which it will stop returning YES depends on where the object is in the process of being deleted.
好吧,我书中的设计选择很糟糕,但我很好奇人们是如何解决这个问题的。显然我需要在删除之前清除观察到的属性。我能想到的唯一“干净”方法是观察 NSManagedObjectContextWillChange 事件并查看我是否正在存储任何已删除的对象。看起来像是一个干净的解决方法,但也许我遗漏了一些东西。
最佳答案
另一种选择可能适用于您的设计,也可能不适用于您的设计:-[NSManagedObject managedObjectContext]
将返回 nil
如果它已被删除并且上下文已保存。您可以在收到 NSManagedObjectContextDidSave
通知后进行检查。
关于objective-c - NSManagedObject 和 isDeleted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4748484/