Bot
Bot

Reputation: 11855

iOS: Core Data class method

Is it possible and practical to create a Core Data class method that will return the current instance of managedObjectContext? I am wondering so that I can segue to other controllers and load modal views without having to pass the managedObjectContext.

Also if I am using Core Data with dispatch_async I know I need to create my own instance of managedObjectContext but I can use the same coordinator. Will this make the information accessible both inside the dispatch_async and in the main thread?

I am basically using the dispatch_async to get data from the API and store it while the user is using the application.

Upvotes: 0

Views: 882

Answers (3)

jrturton
jrturton

Reputation: 119242

Dealing with the first part of the question only (you shouldnt really ask multiple questions!) you don't have to pass the managed object context around - presumably you are passing a managed object? In that case the context is available as a property of the managed object itself - .managedObjectContext.

Upvotes: 0

Rodaine
Rodaine

Reputation: 1024

I had a similar issue when trying to asynchronously getting data from my server to the app. My method is a bit different, but basically here it is (this is a 4.3 project, so no ARC):

The following methods are in my DataUpdater singleton. This first method is called at app startup:

- (void) update { //download the updates on a new thread

  [NSThread detachNewThreadSelector:@selector(updateThread) 
                           toTarget:self withObject:nil]; 

}

It initializes a thread with this selector, which is responsible only for downloading the content from the API, then passing it back to the main thread to be saved.

- (void) updateThread { //the actual update thread

  //New thread, new auto-release pool 
  //(dunno if you need to do anything fancy for ARC)
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  //... 
  //YOUR CODE TO DOWNLOAD (BUT *NOT* SAVE) DATA FROM THE SERVER
  //DON'T CREATE ANY MANAGED OBJECTS HERE
  //...

  //Pass the data to the main thread to perform 
  //the commit to the Core Data Model
  [self performSelectorOnMainThread:@selector(saveUpdate:) 
                         withObject:data waitUntilDone:NO];

  //kill the thread & the auto-release pool
  [NSThread exit];
  [pool release];
}

Now that we're back on the main thread, the data is added to the Core Data Model and then the context is saved.

- (void) saveUpdate:(NSArray *) data {

  //add the objects to your Core Data Model

  //and save context
  NSError * error = nil;
  [[[CoreManager defaultCoreManager] CoreContext] save:&error];
  if (error) {
    [NSException raise:@"Unable to save data update" 
                format:@"Reason: %@", [error localizedDescription]];
  } else {
    [[NSNotificationCenter defaultCenter] postNotification:
      [NSNotification notificationWithName:@"DONE" object:nil]];
}

}

Upvotes: 0

Kevin
Kevin

Reputation: 3131

In the past, I've created a Core Data manager singleton class that has simplified things. Here is an example, but this is pre-iOS5/ARC, so some changes need to be made.

Upvotes: 1

Related Questions