Search code examples
cocoacore-datakey-value-observingnsarraycontroller

Cannot remove an observer ... for the key path "..." from ... because it is not registered as an observer


I have a Core Data model with two entities: A and B. A has a relation to one or more Bs. B has a property bValue.

I create many instances of a class and some of them invoke a notification. This notification called a method that creates a new B entity and adds it to A. The instances are created using NSThread in order to make the UI more responsive.

This works only fine then there are not too many notifications invoked. Or at least not too many at 'the same time'.

Then I get this exception:

Cannot remove an observer <NSArrayController 0x10016c150> for the key path "bValue" from <bValue 0x104e55c30> because it is not registered as an observer.

Yes, B is bound to an NSArrayController.

  • If I remove this ArrayController from the NIB file, everything works fine (except everything the ArrayController has to do)
  • If I create many Bs and add them to A elsewhere (outside the notification method) everything works find, also with the ArrayController.

Can someone help me please?

(Please excuse my poor pronunciation.)


Solution

  • You said you're doing this work on a separate thread. If so, you need to make sure you are using a separate NSManagedObjectContext. You cannot use the same NSManagedObjectContext on multiple threads, nor can you use a NSManagedObject associated with one MOC in another MOC. Each thread needs to interact with CoreData independently.