• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to send fetchrequest to main context from a local context on a separate thread?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Mike Abdullah <email@hidden>
  • Date: Thu, 18 Oct 2012 16:19:40 +0100

On 18 Oct 2012, at 16:01, Koen van der Drift <email@hidden> wrote:

> 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?

Here's what you want:

__block BOOL exists;
dispatch_sync(dispatch_get_main_queue, ^{
	NSManagedObjectContext *context = … // grab the context
	exists = … // query the context to see if the data already exists
});


Works on OS X 10.6+ and iOS4+
_______________________________________________

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


  • Follow-Ups:
    • Re: How to send fetchrequest to main context from a local context on a separate thread?
      • From: Koen van der Drift <email@hidden>
References: 
 >How to send fetchrequest to main context from a local context on a separate thread? (From: Koen van der Drift <email@hidden>)
 >Re: How to send fetchrequest to main context from a local context on a separate thread? (From: Koen van der Drift <email@hidden>)

  • Prev by Date: Re: How to send fetchrequest to main context from a local context on a separate thread?
  • Next by Date: Cross-fading between layers
  • Previous by thread: Re: How to send fetchrequest to main context from a local context on a separate thread?
  • Next by thread: Re: How to send fetchrequest to main context from a local context on a separate thread?
  • Index(es):
    • Date
    • Thread