Re: Core Data concurrency during import of folder contents
Re: Core Data concurrency during import of folder contents
- Subject: Re: Core Data concurrency during import of folder contents
- From: Terrence Geernaert <email@hidden>
- Date: Fri, 30 Jan 2015 09:56:48 -0800
I prefer the GCD method but there are a few details that cause people trouble when combining GCD with CoreData.
1. A named GCD queue may guarantee serial processing but it does not guarantee that it will always use the same thread
2. NSManagedObjectContexts are not thread safe, and it’s not just a problem of you avoiding access from two threads at the same time. Only access the MOC from the thread on which it was created.
3 MOCs are light weight, so don’t worry about the premature optimization when creating and destroying them... unless it becomes an issue.
In other words a private MOC is not the complete answer. Inside your block you need to create a new MOC, save it once you’ve completed your long running data collection process and then destroy it at the end of the block. You should have another MOC that you keep around, waiting on the main thread to be made aware of the save, so that you can handle the changes in your FRC. This is not the whole story (you can use child MOCs for a little more sophistication) but this should you going in the right direction.
__
Terrence
> On Jan. 30, 2015, at 9:20 AM, Steve Mills <email@hidden> wrote:
>
> I'm trying to learn Core Data. I have a working document model hooked up to a table view (OS X). I have an import method that uses FSDirectoryEnumerator to add an object for each file in the folder. Oky doke! But that blocks for a few seconds while it loops. So I'm trying to make it threaded. One example I saw uses GCD. It kinda seemed to work when I had a bunch of printfs in there so I could watch it progress, but when I took them out it just crashed at some point, and it still blocked any sort of user events.
>
> Next I tried sticking the entire import operation into a block via NSOperationQueue's addOperationWithBlock:. That throws because some set was being changed by multiple threads. Then I tried the suggestion of using a private NSManagedObjectContext in the block. That got me somewhere, but of course the data didn't populate my table. I tried the NSManagedObjectContextDidSaveNotification trick, but I couldn't get that to work.
>
> Reading the Concurrency with Core Data page online says the information is outdated. Where do I find current documentation? Or better yet, an actual example that actually works? My brain is about to explode, and nobody wants to clean that up.
>
> --
> Steve Mills
> Drummer, Mac geek
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden