4

When I insert a new object in a Core Data managed object context and shortly after that try to find this new object in the NSArrayController (which is connect with the managedObjectContext through binding), I can't find it. I do the creation and search in one method.

My question now. How long does it take for a new inserted object to show up in the NSArrayControllers arrangedObject array?

Update: Here is the code for inserting and fetching the new objects

NSEntityDescription *entity = [[[self managedObjectModel] entitiesByName] objectForKey:@"EntityName"];
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:[self managedObjectContext]];
...
[[self managedObjectContext] processPendingChanges];

[arrayController fetch:nil];
NSArray* objects = [arrayController arrangedObjects]; //the new object is not present in the array
brutella
  • 1,597
  • 13
  • 26

2 Answers2

3

It's not a matter of "how long" but "at what point". There's enough of a distinction that it's important to study it. :-)

Usually array controllers are automatically updated (re-fetch their contents in this case) on the next run-loop but technically "at some future run loop". If you want them to update immediately after inserting something, send your MOC a -processPendingChanges then ask the array controller to -fetch:.

Among the first things you read in the Core Data documentation is that it's an advanced Cocoa topic whose prerequisite knowledge includes Key Value Binding and Key Value Observing. The missing bit of knowledge that led you to this question is found in understanding of KVC/KVO (and the Cocoa Bindings layer).

Besi
  • 22,579
  • 24
  • 131
  • 223
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • The processPendingChanges method call didn't work. I will read through the KVC/KVO docu - maybe this helps me to understand the problem. Thx – brutella Dec 27 '10 at 18:32
  • Well that only "finishes" the insertion (often not necessary but doesn't hurt). The call to -fetch: is critical. Did you do that too? Also, I'm assuming you have only one MOC. If you *are* calling -fetch: then you may need to post your code (insertion + this forced array controller fetch). – Joshua Nozzi Dec 27 '10 at 18:57
  • Looks fine to me. What are all the settings on your array controller? Any filter predicates? – Joshua Nozzi Jan 02 '11 at 19:27
  • There are some filter predicates but they have no effect to the inserted object because the object appears "after some time". – brutella Jan 03 '11 at 15:46
  • sorry guys, but why avoid simply adding the object to the arrayController? it has -(void) addObject:(id)newObject; method, that does the trick without further ado. One thing it does not - however, is to reveal the new selection. Also you can configure an NSArrayController to automatically rearrange objects (e.g. when new objects are added) – Motti Shneor Nov 09 '15 at 08:24
  • Because a) it doesn't address the OP's actual question (due to the selection issue you mentioned yourself), and b) it's not always possible to add things to a collection via an array controller's methods (because you may need to do something special like record a conplex undo group, etc.). I chose to explain the issue so readers can understand the behavior. – Joshua Nozzi Nov 09 '15 at 12:38
  • I had a similar issue and calling `processPendingChanges` on my MOC did the trick. I did not have to explicitly `fetch`. Thanks! – Besi Jan 30 '16 at 01:32
  • I'm struggling with similar issue, and I can't find a reliable way to resolve it, even after reading this and trying to apply your solution. BTW - Cocoa bindings (and KVO/KVC protocols) are all synchronous, meaning, never deferred to another runloop, so unless CoreData is doing something very strange - the issue is not related to KVO/KVC "latency". – Motti Shneor Jan 17 '18 at 04:57
  • @MottiShneor You are conflating Cocoa Bindings and Core Data. Don’t do that. – Joshua Nozzi Jan 17 '18 at 18:49
0

Just found a fix for this. I use the setSelectedObjects: method of the NSArrayController to select the object. Don't know why I didn't used this method anyway!

brutella
  • 1,597
  • 13
  • 26
  • I had a similar issue as I'd missed the requirement to call -fetch: on the NSArrayController. I note that -setSelectedObjects: seems to do this as a side-effect but for belt-and-braces I chose to send both methods to the controller. The latter is useful so that bindings in an editor sheet for the managed objects in question would work whether a new item was being added, or an old item was being edited, though it feels like this is a bit of a hack and a better approach may exist. – Andrew Hodgkinson Jan 20 '11 at 16:52
  • How can "setSelectedObjects" help, or work, if the object is not yet in the arrayController?'s content array? – Motti Shneor Jan 17 '18 at 04:59