DCTCoreDataStack icon indicating copy to clipboard operation
DCTCoreDataStack copied to clipboard

Update DCTCoreDataStack.m

Open radianttap opened this issue 12 years ago • 7 comments

Fixes a crash on startup in situation when two threads (two MOCs) attempt to access PSC while it's still being created.

radianttap avatar Sep 27 '13 20:09 radianttap

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

danielctull avatar Sep 28 '13 12:09 danielctull

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 avatar Sep 29 '13 14:09 mikeabdullah

@mikeabdullah: Yup, completely agree.

danielctull avatar Sep 29 '13 16:09 danielctull

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.

danielctull avatar Sep 29 '13 16:09 danielctull

Daniel, did you see the test fail when you created it before you fixed it?

Abizern avatar Oct 12 '13 19:10 Abizern

Yes.

danielctull avatar Oct 12 '13 20:10 danielctull

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.

danielctull avatar Oct 12 '13 20:10 danielctull