How to add observers to an NSManagedObject?

1.5k Views Asked by At

What is the preferred approach to registering observers on an NSManagedObject (since Core Data "'owns' the lifecycle" of managed objects)?

Am I correct that the way to do this is to listen for NSManagedObjectContextObjectsDidChange-Notification and call addObserver:... for NSInsertedObjectsKey and removeObserver:... for NSDeletedObjectsKey?

2

There are 2 best solutions below

3
On

Yeah, you're correct. Observing the MOs directly will lead to trouble, for reasons you have already posted in your question (within the parentheses).

0
On

In order to observe MOs throughout their lifecycles, add observers when they come into existence, with awakeFromInsert and awakeFromFetch.