Update DCTCoreDataStack.m
Fixes a crash on startup in situation when two threads (two MOCs) attempt to access PSC while it's still being created.
Thanks for the pull request. I can see the issue is to do with accessing the managedObjectContext property from multiple threads at the same time. I've never come across this, I presume this is due accessing DCTCoreDataStack from the main thread.
I have added a unit test (although a poor one I admit) and fix on the threading branch. You can see my solution uses a private queue, which I think would be preferable going forward as we might want to use it for synchronising other parts of the setup. Could you check to see whether this solution resolves your issue?
Thanks again, Daniel
I propose a simpler approach: Create the PSC (and likely main MOC too) in -init…. This removes the need for any locking/scheduling in the getter methods.
@mikeabdullah: Yup, completely agree.
Except, I've just realised that DCTCoreDataStack allows you to set an error handler for the loading, which won't be set until after init is returned. :-/ So it seems the queue is needed.
Daniel, did you see the test fail when you created it before you fixed it?
Yes.
As I explained in the commit message https://github.com/danielctull/DCTCoreDataStack/commit/894af35c2a66961b803ae12d8da1f19e3d1439fd, it doesn't always fail, but because it's a race condition, I'm not sure there is a valid test for it.