0

I need to basically update my core data in a background thread without blocking UI and save it. After saving should reload table View to view the changes. So for doing this I thought of using

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Add code here to do background processing
   NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    self.backgroundManagedObjectContext = context;
    if(self.managedObjectContext == nil)
        self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    self.backgroundManagedObjectContext.parentContext = self.managedObjectContext;

    //update data
    [self getDataFromFile];

dispatch_async( dispatch_get_main_queue(), ^{
        // Add code here to update the UI/send notifications based on the
        // results of the background processing
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];
        [context release];
    });
});

and in getDataFromFile when I try to fetch data

if(![NSThread isMainThread])
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:self.backgroundManagedObjectContext];
    [request setEntity:entity];
    logs = [self.backgroundManagedObjectContext executeFetchRequest:request error:nil];
}

I get error * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'LogDetails''

can anyone explain why I am getting this error

Also I have an another doubt whether to include it as background managedObjectContext or child managedObjectContext with parent as main thread managedObjectContext

aparna
  • 353
  • 2
  • 3
  • 13
  • I would suggest that you read this great tutorial: http://www.cocoanetics.com/2012/07/multi-context-coredata/ – rckoenes Mar 07 '13 at 11:20
  • I read few links http://stackoverflow.com/questions/2138252/core-data-multi-thread-application, http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html, http://www.cimgf.com/2011/05/04/core-data-and-threads-without-the-headache/ and still more including the link http://www.cocoanetics.com/2012/07/multi-context-coredata/ but still I am not able to find where I m going wrong – aparna Mar 07 '13 at 11:27
  • Where do you set the parentObject? like ` self.backgroundManagedObjectContext = main.managedObjectContext` als there is not need to the set `PersistentStoreCoordinator` when using the `NSPrivateQueueConcurrencyType` and you should use `[self.backgroundManagedObjectContext performBlock:(block)]` – rckoenes Mar 07 '13 at 11:34

1 Answers1

1

One NSManagedContext should only be used in one thread. passing NSManagedObject between threads are potentially unsafe.

CarmeloS
  • 7,868
  • 8
  • 56
  • 103