Search code examples
uitableviewcore-datakey-value-observing

KVO observing of NSMangedObject in a UITableViewCell


I'm using a custom UITableViewCell to display properties of a NSMangedObject from my model. Each cell holds a reference to the managed object.

I also listen to changes in properties of the object by calling addObserver:… on the object.

The problem is that the objects can be removed from the model, and I want to unregister when this happens.

There is a similar question here, but the suggested solution there is to use prepareForDeletion on the object, and then manually notify the cells. This is not a very pretty solution.

I was wondering what is the recommended solution for these cases, and more then that - is observing KVO changes on the managed object is indeed the best approach to what I am trying to achieve.


Solution

  • No, you should not use KVO for this. You need to use an NSFetchedResultsController, so that your tableview cells will be automatically updated using the NSFetchedResultsController delegate methods.

    Please see the relevant documentation.