Multithreaded Core Data p>
NSTreeControllerand NSOutlineViewwith bindings
NSTreeController
NSOutlineView
Creates child objects in NSOperation in the background context
Combines into the main context using mergeChangesFromContextDidSaveNotification
mergeChangesFromContextDidSaveNotification
If I complete 20 operations of creating children, as soon as the mergers are completed, I see only about 10-15 children in the outline representation.
If I set the maximum parallel operations to 1, it works fine and I see 20 children.
Am I trying to do something impossible? I see how master data can successfully merge. Or is there a problem with my code?
JGGroupController
-(id)init { self = [super init]; queue = [[NSOperationQueue alloc] init]; [queue setMaxConcurrentOperationCount:10]; // If this is 1, it works like a dream. Anything higher and it bombs. return self; } -(IBAction)addTrainingEntryChild:(id)sender { moc = [[NSApp delegate] managedObjectContext]; JGTrainingBase *groupToAddTo = [[tree selectedObjects] objectAtIndex:0]; for (NSUInteger i = 0; i < 20; i++) { JGAddChildrenObjectOperation *addOperation = [[JGAddChildrenObjectOperation alloc] init]; [addOperation addChildObjectToGroup:[groupToAddTo objectID]]; [queue addOperation:addOperation]; } }
JGAddChildrenObjectOperation - subclass of NSOperation
-(id)addChildObjectToGroup:(NSManagedObjectID *)groupToAddToID_ { groupToAddToObjectID = groupToAddToID_; return self; } -(void)main { [self startOperation]; JGTrainingBase *groupToAddTo = (JGTrainingBase *)[imoc objectWithID:groupToAddToObjectID]; JGTrainingBase *entryChildToAdd = [JGTrainingBase insertInManagedObjectContext:imoc]; [groupToAddTo addChildren:[NSSet setWithObject:entryChildToAdd]]; [imoc save]; [self cleanup]; [self finishOperation]; } -(void)mergeChanges:(NSNotification *)notification { NSManagedObjectContext *mainContext = [[NSApp delegate] managedObjectContext]; [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES]; } -(void)startOperation { // Omitted - Manage isExecuting, isPaused, isFinished etc flags imoc = [[NSManagedObjectContext alloc] init]; [imoc setPersistentStoreCoordinator:[[NSApp delegate] persistentStoreCoordinator]]; [imoc setUndoManager:nil]; [imoc setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy]; [imoc setStalenessInterval:0]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:imoc]; } -(void)finishOperation { // Omitted - Manage isExecuting, isPaused, isFinished etc flags }
"" . :
2 , O: F O: G, F G 1, G: 1 [A, B, C, D, E ].
O: F O: G, G: 1.
O: F G: 1 [A, B, C, D, E, F]. O: G G: 2 [A, B, C, D, E, G].
, , [A, B, C, D, E, F], [A, B, C, D, E, G], .
, CoreData , , . .
, , . , 20 , 1 , 20 , , .
.
Source: https://habr.com/ru/post/1774593/More articles:How can I get the port number of a newly accepted connection in python? - pythonThe state of threads when starting a signal handler - multithreadinghttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1774590/get-tabs-to-go-to-the-right&usg=ALkJrhhBQdIJK66EHSzR-E_ttbRP-tQygQWhat is the difference between a transaction manager and a database manager? - databaseCreating Custom Flags #warning - c ++SharePoint BCS SSS Help - sharepointMeasuring the similarity between a color image (OpenCV) - c ++How to map partitioned data using NHibernate - mappingexport keyword in bash scripting - bashHow to find all peaks and troughs of tidal data? - phpAll Articles