NSManagedObjectContext executeBlockAndWait causes a deadlock when called from two threads

I have an OSX application where I use the NSManagedObjectContext setting for the parent / child object. The child MOC has an NSPrivateQueueConcurrencyType and one that I use first. Parent is NSMainQueueConcurrencyType

When I call performBlockAndWait in a child context from the background thread at the same time as the call from the main thread, I get a dead end - semaphore_wait_trap . Pausing debugging shows that both threads are stuck in performBlockAndWait

How can I get around this? I thought this method was designed specifically for this situation and would just put the blocks in a private context queue and then return accordingly?

+4
source share
2 answers

I worked on this by creating another queue, and then made all my executeBlock calls through this to make sure that they would not be confused with each other. Honestly, I'm not sure if this is a good practice, but it solved the problem for my specific situation.

+2
source

NSManagedObjectContext is still not thread safe even with private concurrency types.

execute Block: and execute BlockAndWait: only ensure the execution of block operations in the queue specified for the context.

You can still get deadlocks with executeBlockAndWait: because it will block the current executable thread until it returns. What happens inside performBlockAndWait :? Perhaps something that requires access to the main thread, so it has come to a standstill.

Can you use performBlock: instead?

+1
source

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


All Articles