3

I have a Document Based Core Data Application. Each NSPersistantDocument has its own Managed Object Context.

I can open/create a document and create a Managed Object within fine, but after open/creating a document and then initialising and inserting a managed object within that document, created Managed Objects from any other document are not registered by the NSFetchedResultsController:

I'm sure I'm missing something significant.

I can see I have changes with:

managedObjectContext.hasChanges

And can check they are indeed the objects with:

managedObjectContext.insertedObjects

It looks like NSFetchedResultsController is the problem as when observing NSManagedObjectContextObjectsDidChangeNotification I get notifications for the inserted managed objects, but my NSFetchedResultsController Delegate implementing:

controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:

gets nothing...

I don't suppose anyone has any ideas?

Sammy The Hand
  • 175
  • 1
  • 11
  • How do you insert an object? What do you mean by "refuses to update" and "created Managed Objects from any other document are not processed as pending changes"? Each document has it's own managedObjectContext with it's own managed objects. You cannot move them around. – Willeke Apr 09 '18 at 08:28
  • Manage Objects are made with: let managedObject = Entity(context: self.managedObjectContext) - which should initialise and insert into the MOC. This is done on the main thread so should be processed on the main event loop. As I'm using FetchedResultsControllers it should trigger: func controller(_ controller: NSFetchedResultsController, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { }, which it does normally unless I have multiple documents... – Sammy The Hand Apr 09 '18 at 11:38
  • So it works with one open document but not with two open documents? Do you use the correct context everywhere? – Willeke Apr 09 '18 at 14:35
  • That's correct. I believe so each document should be using it's own context and I've checked they're not trying to use each others.. – Sammy The Hand Apr 15 '18 at 10:39
  • Did you start your project with the Xcode template for document-based apps with Core Data? You should then have a document class. Do you create your managed objects from within this class? – Dirk Apr 30 '18 at 18:26
  • Yes the project was started as an Xcode template for document based app. I have not implemented my own creation of the managed object context. On creation of a new document window I simply retrieve the context guard let context = self.managedObjectContext else { return } and inject that into the variety of model managers that need it. – Sammy The Hand Jun 06 '18 at 08:02
  • On further investigation it looks like FetchedResultsController is not updating as NSManagedObjectContextObjectsDidChangeNotification still observe the changes... – Sammy The Hand Jun 06 '18 at 08:08

0 Answers0