如何做完美的KVO为NSManagedObject? [英] How to do perfect KVO for NSManagedObject?
问题描述
完美的KVO包括两部分:正确添加观察者并正确移除观察者。
Perfect KVO here includes two parts: add observer correctly and remove observer correctly.
故事:
- 我使用一个UITableViewCell(单元格)来显示一个NSManagedObject(对象)。
- 每个对象都有一些动态属性, >
-
并非所有对象都具有相同的观察属性集。我添加关键路径观察者选择性地像这样:
- I use one UITableViewCell(cell) to display one NSManagedObject(object).
- Each object has some dynamic properties that need observing by its cell.
Not all objects have the same set of observed properties. I add key path observers selectively like this:
if(object.thumbnail_pic_url)
[object addObserver:cell forKeyPath:@thumbnail_pictureoptions: NULL];
if (object.thumbnail_pic_url) [object addObserver:cell forKeyPath:@"thumbnail_picture" options:0 context:NULL];
对象可以删除。当对象被删除时,我必须删除观察者。数据库非常大和复杂,所以我绝对不想注册所有单元格接收mocat通知像NSManagedObjectContextObjectsDidChangeNotification。但是我可以接受在对象中添加一个单元格ivar,如果我必须,即使它再次是好的Modle-View-Controller设计模式。
Object could be deleted. I must remove observers when object is deleted. The database is very large and complex so I definitely don't want to register all cells to receive moc notifications like NSManagedObjectContextObjectsDidChangeNotification. But I can accept to add a cell ivar in object if I have to, even though it goes agains good Modle-View-Controller design pattern.
问题:如何从一个对象中删除所有注册关键路径的观察者(单元格)?
The problem: How can I correctly remove the observer(cell) for all the registered key paths from an object when it is deleted?
事实上,这是一个大问题,可以分为两个小问题:
In fact, it is a big problem that can be divided into two small problems:
- 在哪里可以放置观察者删除代码?
-
如何确定要取消注册的键路径?我不能在一个对象被删除后查询它的属性 - 它会导致无法实现的错误,所以我不能这样编写代码:
- Where is the best place to put the observer removing code?
How do I determine which key paths to unregister? I can't query its properties after an object is deleted — it will cause unfulfillable faults, so I can't write code like this:
if(object.thumbnail_pic_url)
[object removeObserver:cell forKeyPath:@thumbnail_picture];
if (object.thumbnail_pic_url) [object removeObserver:cell forKeyPath:@"thumbnail_picture"];
blindly remove observer for unregistered key path - exceptions(不能删除关键路径thumbnail_picture的观察者,因为它没有注册为观察者。)将被抛出。
and I can't either blindly remove observer for unregistered key path — exceptions(Cannot remove an observer for the key path "thumbnail_picture" from because it is not registered as an observer.) will be thrown up.
推荐答案
an0,
有一个NSManagedObject方法只用于删除定时函数:-prepareForDeletion。
There is an NSManagedObject method just for doing deletion timed functions: -prepareForDeletion.
其文档声明:您可以实现此方法以执行删除对象之前所需的任何操作,例如关系关闭之前的自定义传播或对象的重新配置使用键值观察。
Its documentation claims: "You can implement this method to perform any operations required before the object is deleted, such as custom propagation before relationships are torn down, or reconfiguration of objects using key-value observing."
您还可以使用:-willTurnIntoFault和-didTurnIntoFault。但我认为你会更乐意使用-prepareForDeletion。
You could also look at using: -willTurnIntoFault and -didTurnIntoFault. But I think you'll be happier using -prepareForDeletion.
Andrew
这个方法记录在类引用中。我谨建议您通过阅读文档节省时间。
P.S. This method is documented in the class reference. I respectfully suggest that you save time by reading the documentation.
这篇关于如何做完美的KVO为NSManagedObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!