4
votes

I have an OSX app where I'm using a parent/child NSManagedObjectContext setup. The child MOC has NSPrivateQueueConcurrencyType and is the one that I'm primarily using. The parent is set to be NSMainQueueConcurrencyType

When I call performBlockAndWait on the child context from a background thread at the same time as it is called from the main thread I get deadlock – semaphore_wait_trap. Pausing the debugger shows both threads are stuck at performBlockAndWait

How can I get around this? I thought this method was designed for exactly this situation and would simply queue up the blocks on the context's private queue then return appropriately?

2

2 Answers

2
votes

I worked around this by creating a serial queue and then doing all my performBlock calls through this to make sure they wouldn't mess each-other up. Honestly, I'm not sure if this is good practice but it did solve the problem for my particular situation.

1
votes

NSManagedObjectContext's are still not thread safe, even with the private concurrency types.

performBlock: and performBlockAndWait: only ensure the block operations are executed on the queue specified for the context.

You can still get deadlocks with performBlockAndWait:, as it will block the currently executing thread until it returns. What is happening inside the performBlockAndWait:? Probably something that requires access to the main thread, which is why it is deadlocked.

Can you use performBlock: instead?