2011-01-27 3 views
2

안녕하세요, 내가 CoreData과 동시성 몇 가지 문제가업데이트는 백그라운드 스레드에서 개체를 관리하고 주 스레드에서 그들을 표시, 무슨 일이

잘못, 그래서 나는 간단한 코드를 시도하고 그것은 여전히 ​​작동하지 않습니다. 내가 틀린 곳을 알려 주실 수 있겠습니까?

나는 내 CoreData 개체의 내용을 표시하는 뷰 컨트롤러가

@implementation OBSDataManager 

@synthesize persistentStoreCoordinator; 

- (OBSDataManager *)initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)aPersistentStoreCoordinator { 
    if (self = [super init]) { 
     self.persistentStoreCoordinator = aPersistentStoreCoordinator; 
    } 

    return self; 
} 

- (void)dealloc { 
    [persistentStoreCoordinator release]; 

    [super dealloc]; 
} 

- (void)start { 

    [self performSelectorInBackground:@selector(updateData) withObject:nil]; 
} 

- (void)updateData { 
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
    context.persistentStoreCoordinator = self.persistentStoreCoordinator; 

    // get chunk if it exists, or create it 
    OBSChunk *chunk = [OBSChunk theChunkInContext:context]; 
    if (!chunk) { 
     chunk = [NSEntityDescription insertNewObjectForEntityForName:@"Chunk" 
              inManagedObjectContext:context]; 
    } 

    while (1) { 
     // update content 
     chunk.text = [[NSDate date] description]; 

     // save it 
     NSError *error; 
     if ([context save:&error]) { 
     NSLog(@"Problem on save"); 
     } 
    } 
    [context release]; 

} 

@end 

는 "DataManager에"갱신 한 CoreData 객체를 생성

@implementation MainViewController 

@synthesize managedObjectContext; 
@synthesize label; 

#pragma mark - 
#pragma mark UIViewController 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(onCoreDataUpdate:) 
               name:NSManagedObjectContextDidSaveNotification 
               object:nil]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:NSManagedObjectContextDidSaveNotification 
               object:nil]; 
} 


#pragma mark - 
#pragma mark private 
- (void)onCoreDataUpdate:(NSNotification *)updateNotification { 
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:updateNotification]; 

    OBSChunk *chunk = [OBSChunk theChunkInContext:self.managedObjectContext]; 

    self.label.text = chunk.text; 
} 

@end 

때문이다 청크 개체를 가져올 것으로 보인다 onCoreDataUpdate 메소드에서 데이터에 오류가 발생했습니다.

어디서 잘못 되었나요?

감사합니다,

답변

6

-onCoreDataUpdate: 쿠엔틴은 배경 스레드에서 호출되고있다. 알림은 보낸 스레드에서 수신됩니다. 실제로 업데이트를 처리하려면 메인 스레드로 콜백해야합니다. (응용 프로그램이 더 이상 충돌하지 않습니다)

- (void)onCoreDataUpdate:(NSNotification *)updateNotification { 
    if (![NSThread isMainThread]) { 
     [self performSelectorOnMainThread:_cmd withObject:updateNotification waitUntilDone:NO]; 
     return; 
    } 
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:updateNotification]; 

    OBSChunk *chunk = [OBSChunk theChunkInContext:self.managedObjectContext]; 

    self.label.text = chunk.text; 
} 
+0

감사합니다, 작동 을,하지만 난 디버거에서 개체를 인쇄 할 때, 데이터는 여전히 "오류"로 표시됩니다 : 당신은이를 처리하기 위해 다음과 같은 것을 사용할 수 있습니다. : – Quentin

+0

오브젝트가 결함이있는 것은 무엇입니까? 오브젝트는 데이터에 액세스 할 때까지 기본 데이터가 폴트되지 않습니다 .CoreData에서 폴트가 정상적인 작동 부분입니다. –

+0

자, "fault" 인상적입니다. : p – Quentin

관련 문제