I am writing a function that does some CoreData stuff. I want the function to return only after all CoreData operations have been completed. CoreData stuff involves creating an object in the background context, and then performing some additional actions in the parent context:
+ (void) myFunction NSManagedObjectContext *backgroundContext = [DatabaseDelegate sharedDelegate].backgroundContext; [backgroundContext performBlockAndWait:^{ MyObject *bla = create_my_object_in:backgroundContext; [backgroundContext obtainPermanentIDsForObjects:[[backgroundContext insertedObjects] allObjects] error:nil]; [backgroundContext save:nil]; [[DatabaseDelegate sharedDelegate].parent.managedObjectContext performBlockAndWait:^{ [[DatabaseDelegate sharedDelegate].parent updateChangeCount:UIDocumentChangeDone];
I want the return to happen only after [queue addOperation:someOperation] . This seems to work in most cases, but I had one case where this function never returned. He seemed to be at a standstill, and I suspect that this is due to performBlockAndWait .
My questions:
(1) Can someone explain why this dead end occurs?
and
(2) What is the correct way to achieve the same functionality? The requirement is that myFunction returned only after both blocks have been executed.
Thanks!
ios objective-c deadlock core-data nsmanagedobjectcontext
user1013725
source share