I have an iOS app that pulls user data down from a web service and saves it to a sqlite backed core data store. While this is also going on I’m taking the url for the user’s avatar and saving the image locally into core data as the download of each image becomes available.
Each of these are performed on their own thread with their own managed object context but with the same shared persistent store coordinator.
When there are just a few items being pulled down, this works great, but as we approach 100 items or so I get frequent deadlocks on the initial load of data. When I pause the debugger I see the different threads usually both waiting on an executeFetchRequest.
I turned on the sql debug in my scheme and according to the console output the fetches complete, but the threads never continue.
What else can I use or look at to examine why these are deadlocking or how I can prevent these deadlocks from occurring?
Make sure that you batch changes to the DB together. No need to call saveContext after each change. Instead it is probably sufficient to save in the temporary background context after every dozen or so changes.
Please see this article of mine on some pointers: http://www.cocoanetics.com/2012/07/multi-context-coredata/