Re: How to send fetchrequest to main context from a local context on a separate thread?
Re: How to send fetchrequest to main context from a local context on a separate thread?
- Subject: Re: How to send fetchrequest to main context from a local context on a separate thread?
- From: Koen van der Drift <email@hidden>
- Date: Thu, 18 Oct 2012 11:01:27 -0400
After some more searching I came to the following plan.
1. get the main context from another thread as follows:
AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
2. use performSelectorOnMainThread to execute the fetch request:
[mainContext performSelectorOnMainThread:@selector(executeFetchRequest:)
withObject:fetchRequest
waitUntilDone:YES];
However, performSelectorOnMainThread does not give a return value
back, and the selector should really be executeFetchRequest:error:
One way to solve this would be to use an NSInvocation, I think? Or
maybe wrap the executeFetchRequest in another method, call
performSelectorOnMainThread on it, and then use a notification to send
the result back.
Will try this later, does any of these approaches sound reasonable?
- Koen.
On Wed, Oct 17, 2012 at 12:25 PM, Koen van der Drift
<email@hidden> wrote:
> I'm reading up on importing data into the CD store using multiple
> contexts and threads.This article has already been very helpful:
> http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/
> Now if I have a local context on a separate thread to import my data,
> how can I send a fetchrequest to the main context to see if a certain
> entity already exists, and thus skip the import? I already have the
> fetchrequest code, just trying to understand how to access the main
> context form a separate thread.
>
> Thanks,
>
> - Koen.
_______________________________________________
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