performBlockAndWait On Child Context with Private Queue Deadlocks Parent on iOS 7

半腔热情 提交于 2019-12-05 06:50:52

Who is calling -updateItems:? If that is coming in on the Main Queue you have an issue right there because you are blocking it.

Assuming that is not the case, can you share the thread stack from Xcode that shows the deadline? Specifically with the queues expanded and the main queue expanded?

I will update my answer once I get a good look at that stack.

Quellish is correct that you are not inserting into the child properly. Any activity on that child MOC should be inside of a -performBlock: or -performBlockAndWait:. I would expand the -performBlockAndWait: to cover the entire creation and decision for the object instead of just the save.

Update 1

What does -createWithAttributes:inManagedObjectContext:error: do? Seems like that method is doing something inappropriate. Maybe trying to force permanent IDs or something?

Update 2

As suspected, your -createWithAttributes:inManagedObjectContext:error: is your issue. When you call -objectWithID: you are causing a fetch to fire all the way down at the NSPersistentStoreCoordinator which in turn causes a lock.

Further, this method does not do anything helpful. There is absolutely no value in creating a context just to create an object and then immediately grab the object in another context. All harm, no good. Remove it completely and just create the object in the context where you are actually going to use it. Save or toss it from the context it is being used in.

Don't be clever.

From looking at your code i see you have 2 [childContext performBlockAndWait:^{ that are nested. Removing one of them should clear your issue in ios7. The code is already running in that thread you dont have to do it again.

Always check if you have any nested performBlocks for the same context. This has caused my app to deadlock before in ios7 and work in ios8

The way to check is when you see the deadlock, press pause in debugger and see what blocks all the threads are running. Look at that specific code and check for the nested blocks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!