Update: This thread identifies a bug in NSCollectionView when the represented objects of the collection view are NSManagedObjects. The bug is triggered as follows;
(a) Delete objects from the NSArrayController (b) Perform save on the related nsmanagedobjectcontext any time after the deletion and before the NSCollectionView has finished its animation.
These projects on github demonstrate the issue.
https://github.com/artifacts/NSCollectionViewCoreDataBug https://github.com/iracooke/CoreDataCollectionViewCrashing
Original question below
I have an NSCollectionView setup with its content binding bound to arrangedObjects of an NSArrayController of core-data entities. In my Collection Item View (the prototype for the NSCollectionView view) I have several controls bound via representedObject of my collectionview item to my core-data entities.
For the most part this works OK.
I encounter an objc_exception when I try to delete entities from my ArrayController. I delete these entities simply by calling;
[myArrayController removeObject:managedObjectToDelete];
Unfortunately, when I do this I frequently get a "CoreData could not fulfill a fault" Error .. with the responsible entity being one of the entities being managed by the NSArrayController.
Examination of the call-stack when the exception is thrown reveals that the crash originates when the NSCollectionView receives its _endOfAnimation method. This in turn initiates other methods to do with unbinding (likely of properties of my entity with controls in my view).
One further bit of info is that the Entity I'm working with has no relationships to other entities in my model.
It looks to me as though the following problem is occurring;
- When I delete objects from my NSArrayController they are in turn deleted from the context.
- After deletion from the context the objects are turned into faults
- The NSCollectionView has retained references to the objects (now faults). It tries to clean them up at the end of its animation (unbinding etc).
- When NSCollectionView tries to cleanup bindings to the object it causes core data to attempt to fire a fault on the object (hope I got my terminology right there). This causes an error because the object has not yet been saved to disk.
The only way I can think of to prevent this, is to persist the objects in the store (by saving them) prior to deletion. This works, but only in a hackish kind of way since I need to ensure that one round of deletion is complete before saving again ... and since the error occurs during an animation .. after some delay .. and two saves in succession would result in the same error occuring again.
Does this mean that I cannot used a Core-Data backed NSArrayController to populate an NSCollectionView? If not what am I doing wrong? Is there a better way around this issue?