Re: Deadlock in core data fetch?
Re: Deadlock in core data fetch?
- Subject: Re: Deadlock in core data fetch?
- From: Mike Abdullah <email@hidden>
- Date: Sun, 27 Jan 2013 19:50:39 +0000
On 27 Jan 2013, at 15:04, Martin Hewitson <email@hidden> wrote:
>>>
>>>
>>> 1) How can I interpret the call graph shown below? I'm finding it difficult to extract from that who's blocking whom.
>>> 2) Is there something glaringly wrong with the strategy I sketched above? (I guess so, since it took a lot of experimentation to get it working at all.)
>>
>> From your sample, I would judge that the main thread is waiting to acquire the persistent store coordinator's lock. The worker thread is stuck trying to deliver a message to the main thread, which it can't because the main thread is currently tied up.
>>
>> Now, you didn't explain too much what your background operations are doing. Are they accessing any managed objects at all?
>
> My managed objects have additional data caches which are properties on the NSManagedObject subclass but are not part of the core data model (is this a bad idea?). One of these data caches is an NSTextStorage which can be updated by text views. The background processes are extracting metadata from the string of the text storage. So I don't think this counts as accessing the managed object, does it? However, I do access another property on the managed object - an NSDate representing the time the text content was last edited. I use this in the background thread to help decide if it's necessary to update the metadata. I also access another boolean property which says if the managed object represents a text file. In principle, neither of these properties needs to be stored in the persistent store, and so don't need to be part of the core data model, but currently they are. Could/would just reading the values cause these issues?
As long as you don't touch any modelled properties, you're within Core Data's contract. But it's very easy to do so by accident. Be careful!
>
>
>
>> If so those managed objects ABSOLUTELY MUST come from a separate dedicated context. You cannot access Core Data from multiple threads without using an appropriate technique.
>
> So to process the objects in the main managed context, I should create a new one, and copy the objects I want to process into this new context, then pass the new context to the background thread? Currently I'm just passing an array of managed objects. The context is never directly accessed in the background threads.
You create a new context that shares the same persistent store coordinator as your main one. If the main context has been saved, that'll both contain the same objects; you just need to use the object IDs to locate the objects in the secondary context.
Alternatively, you could setup the second context to be a child of the main context. Or give them both the same parent context.
In short, make sure you read the Core Data threading documentation!
_______________________________________________
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