Using -com.apple.CoreData.ConcurrencyDebug 1 when an asynchronous block fails in both MainContext and PrivateContext

I added the -com.apple.CoreData.ConcurrencyDebug 1 flag to my target. The problem is that I have a background block that works, and it crashes when I use either NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType

My code is:

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSManagedObjectContext *privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [privateManagedObjectContext setParentContext:_mainContext]; [User fetchUserWithContext:_ privateManagedObjectContext]; }]; 

I also tried:

 dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [User fetchUserWithContext:_mainContext]; }]; 

In both situations, I get:

CoreData` + [NSManagedObjectContext Threading_Violation_AllThatIsLeftToUsIsHonor ]:

Thanks for your help,

+5
source share
1 answer

This is not how the Core Data concurrency queue works. When using NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType you need to use the performBlock or performBlockAndWait on the NSManagedObjectContext whenever you access the context. The fact that you are using a GCD here does not matter; if you use the concurrency queue, you must use these methods, or you are doing it wrong.

You need to put your call to fetchUserWithContext inside the block passed to one of these methods. You also need to put something else that accesses objects retrieved from the context inside the block passed to one of these methods.

+7
source

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


All Articles