Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do perfect KVO for NSManagedObject?

Perfect KVO here includes two parts: add observer correctly and remove observer correctly.

The story:

  1. I use one UITableViewCell(cell) to display one NSManagedObject(object).
  2. Each object has some dynamic properties that need observing by its cell.
  3. 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_picture" options:0 context:NULL];

  4. 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:

  1. Where is the best place to put the observer removing code?
  2. 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"];

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.

like image 970
an0 Avatar asked Sep 29 '11 22:09

an0


1 Answers

an0,

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."

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.

like image 51
adonoho Avatar answered Sep 22 '22 18:09

adonoho