2013-03-07 2 views
0

업데이트 된 버전 core data update in background. 링크 Grand Central Dispatch (GCD) with CoreData의 도움으로백그라운드에서 핵심 데이터

는 배경을 만들어 managedObjectContext하지만 핵심 데이터에서 가져 오는 때 오류가 점점 오전

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

}

getDataFromFile 나는 점점 오전 오류

내가 가져 오기 위해 시도

: managedObjectContext

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

같은 오류에서

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

누구든지 내게 왜이 오류가 발생하는지 제안 해 줄 수 있습니까? 이전에는 같은 오류를주는 managedObjectContext 자식을 만들려고했습니다.

미리 감사드립니다.

+0

새 질문을 게시하는 대신 업데이트하려는 내용이있는 원래 질문을 편집하십시오. – Caleb

+0

확인. 미안 해요. 감사 – aparna

답변

1

컨텍스트를 만들고 self.backgroundManagedObjectContext로 설정 한 다음 self.managedObjectContext로 설정하는 이유를 잘 모르겠습니다. 내가 제안하는 것은 귀하의 getDataFromFile 메소드가 어떤 스레드에서든 호출 할 수 있도록 컨텍스트를 취하는 것을 허용하는 것입니다.

당신은

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

그런 다음이

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

당신이 좀 더 도움이 필요하면 알려줘 할 수있을 것입니다.

관련 문제