1

I have a Cocoa App that I have manually added core data to. I setup the table in Interface Builder to list the entities from the data (with NSArrayController), and this is working just fine. The problem is when I insert a new entity (via code) the table does not update until I restart the app.

What do I have to do after inserting an entity and saving the context to get the table to automatically pick up the changes?

1 Answers1

1

I'll assume you mean you want to update your array controller's contents, allowing the table to update as a result.

Short answer: Send your array controller a -fetch: message.

Longer answer: Only entity instances added through an array controller automatically show up in its contents array when it gets its contents via a straight fetch request (ie, when its contents array isn't bound to anything, but rather you set an entity name and a MOC, possibly a predicate, and nothing else).

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Thanks. I tried sending the fetch message after saving and that didn't do anything. Can you point me to some information or samples of how to implement your "longer answer"? I'm new to objective-c and cocoa and am having trouble finding information on topics like these. – Jeremy Gillick Jan 23 '10 at 22:19
  • The longer answer is an explanation of the short answer. :-) After you create an instance of your entity, have you tried asking the managed object context to -processPendingChanges? – Joshua Nozzi Jan 23 '10 at 22:22
  • Is this answer still valid? From what I understand, the result of `-fetch:` is now postponed. In my app, calling this message did not help. Are there any other ways to force an update of the `NSArrayController`? It seems that a `-processPendingChanges` messes up the auto-saving capabilities of core data, so I'd rather avoid that. Any clues? – Carelinkz May 14 '13 at 17:10
  • More information is likely needed - are you using multiple contexts? When are you calling -fetch:? Etc. You may need to post this as a separate question with your specific details (referencing this answer as "not working for you"). – Joshua Nozzi May 14 '13 at 17:30
  • You're probably right. I posted a separate question at http://stackoverflow.com/questions/16550891/how-to-update-an-nsarraycontroller-immediately-after-adding-an-nsmanagedobject-i. – Carelinkz May 14 '13 at 19:06