0

UPDATED Version of core data update in background.

with the help of link Grand Central Dispatch (GCD) with CoreData created a background managedObjectContext but I am getting an error when fetching from core data

-(void) startTimerThread
{
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add code here to do background processing
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
        self.backgroundManagedObjectContext = context;
        [self.backgroundManagedObjectContext setPersistentStoreCoordinator:self.managedObjectContext.persistentStoreCoordinator];
        self.managedObjectContext = self.backgroundManagedObjectContext;
        [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];
        self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    });
});

}

and in my getDataFromFile I am getting error when I try to fetch from managedObjectContext

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setPredicate:[NSPredicate predicateWithFormat:@"date == max(date)"]];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:self.managedObjectContext];

the same error :

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

can anyone suggest me why i m getting this error. Previously I was trying to create a child managedObjectContext which was giving same error.

Thanks in advance.

Community
  • 1
  • 1
aparna
  • 353
  • 2
  • 3
  • 13

1 Answers1

1

I'm not quite sure why you are creating a context and setting it to self.backgroundManagedObjectContext and then setting that to self.managedObjectContext. What I would suggest is allowing your getDataFromFile method to take in a context so you can call it from any thread.

You would have

- (void)getDataFromFileOnContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPredicate:[NSPredicate predicateWithFormat:@"date == max(date)"]];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:context];
}

Then you can do this

-(void) startTimerThread
{
    self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add code here to do background processing
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [context setParentContext:self.managedObjectContext];
        [self getDataFromFileOnContext:context];

    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];        
    });
});

Let me know if you need some more help.

jer-k
  • 1,413
  • 2
  • 12
  • 23