New Core Data object not displayed in NSArrayController organized by objects

When I insert a new object into the context of a managed data object, and shortly after that I try to find this new object in the NSArrayController (which connects to the managed Context object through the binding), I cannot find it. I do the creation and search in one way.

My question is now. How long does it take for a newly inserted object to appear in an array of ordered NSArrayControllers?

Update: Here is the code to insert and select 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 
+4
source share
2 answers

Just found a fix for this. I use the setSelectedObjects: method for NSArrayController to select an object. I don’t know why I didn’t use this method!

0
source

This is not a question of "how long," but "at what point." There are enough differences that it is important to study it. :-)

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

Among the first things you read in the master data documentation is that it is an extended section of Cocoa, the required knowledge of which includes key value binding and Monitoring key value. The missing bit of knowledge that led you to this question can be found in understanding KVC / KVO (and the Cocoa Bindings layer).

+3
source

Source: https://habr.com/ru/post/1333457/


All Articles