2010-12-30 3 views
1

이 코드는 :NSManagedObject를 업데이트하는 방법?

-(void)didEditCard:(NSMutableArray*)theArray { 
    // Create a new instance of the entity managed by the fetched results controller. 
    NSManagedObject *newManagedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath]; 

    // If appropriate, configure the new managed object. 
    [newManagedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"]; 
    [newManagedObject setValue:[theArray objectAtIndex:1] forKey:@"flipCard"]; 

} 

어떤 관리되는 개체 테이블보기에서 건드린 알고. 플레이어가 셀을 터치하면보기가 나타나고 항목을 편집 할 수 있습니다. 그가 done을 클릭하면 위의 메소드가 호출됩니다. 아무 것도 없다. 하지만 setValue를 호출하면 코어 데이터에서 업데이트되지 않습니다.

목표 : 코어 데이터 파일을 업데이트해야하는 관리 대상 객체를 업데이트하려고합니다. 또는 코어 데이터 파일에서 객체를 제거한 다음 똑같은 위치에 새 객체를 추가하려고합니다.

누군가 나를 도와 드릴까요?

편집 :

시도 :하지만 작동하지 않습니다 : (업데이트되지 않습니다)

-(void)didEditCard:(NSMutableArray*)theArray { 
    // Create a new instance of the entity managed by the fetched results controller. 
    NSManagedObject *managedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath]; 

    // If appropriate, configure the new managed object. 
    [managedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"]; 

    NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext]; 
    // Save the context. 
    NSError *error = nil; 
    if (![context save:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 


} 
+0

아무도; [. 뭐가 문제 야? – cocos2dbeginner

+0

theSIndePath는 전역 개체입니까? 이 방법으로 전달하지 마십시오. – Walter

+0

@walter 네 그것은 글로벌 변수이고 그것의 nil이 아닙니다 – cocos2dbeginner

답변

9

대답하기 아주 늦게 보이지만 몇 여전히 사람에 직면하고있다 이 문제는 다음 지침에 도움이됩니다. 데이터베이스에서 레코드를 다시 쿼리를 사용할 수 있습니다

- (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag; 

또는 당신은 coredata가 제공하는 다음과 같은 방법으로 개체를 새로 고침 할 수

. 이렇게하면 문제가 해결됩니다.

중요 포인트 :

열기 샌드 박스에서 SQLite는 (시뮬레이터 작업 디렉토리, 당신은 시뮬레이터에서 테스트하는 경우) 당신이 실제로 반영 않은 변화를 관찰한다.

관련 문제