2012-05-14 4 views
0

처음으로 여기에 질문하는 것이지만 미안하지만 비슷한 것을 찾을 수 없습니다. 그래서 Entity "City"속성 - @ "name"에 업데이트 데이터가 필요합니다. 내 핵심 데이터에서 예를 들어 은 이미 "뉴욕", @ "보스턴"을 보유하고 있습니다. 그리고 XML을 파싱하면 NSMutableArray * Cities = (@ "New York", @ "Boston", @ "Los Angeles", @ "Washington");핵심 데이터 목표 C 엔터티의 콘텐츠 업데이트

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *attributeString = @"name"; 
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
//save to the TableView 
cell.textLabel.text = [[object valueForKey:attributeString] description]; 
if ((indexPath.row + 1) == numberOfSectionsInTableView && (self.isParsingDone)) 
     [self.insertNewObjectToCities:nil]; 
//When coredata updating - tableView is also updating automatically 


//Here is just adding new data, but I do not know how to update 
- (void)insertNewObjectToCities_translation:(id)sender 
{ 
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; 
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context]; 
NSString *attributeString = @"name"; 

if (![[self.parseCities.Cities objectAtIndex:i] isEqualToString:[newManagedObject valueForKey:attributeString]]) 
{ 
    [newManagedObject setValue:[self.parseCities.Cities objectAtIndex:i] forKey:attributeString]; 
    NSLog(@"OBBB %@", [self.parseCities.Cities objectAtIndex:i]); 
    NSLog(@"dessss %@", [[newManagedObject valueForKey:attributeString] description]); 

    i++; 

    if (i==[self.parseCities.Cities count]) 
    { 
     i = 0; 
     return; 
    } 
    else 
    { 
     NSLog(@"valueForKey %@", [newManagedObject valueForKey:attributeString]); 
     [self insertNewObjectToCities_translation:nil]; 
    } 
} 
else 
{ 
    NSLog(@"else"); 
    return; 
} 

// 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. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 

} 

답변

1

관리되는 개체를 업데이트하려면 먼저 개체를 가져 오는 데 사용되는 상황을, 그것을 가져 가져온 NSManagedObject의 필드을 변경 한 다음 저장해야합니다. insertNewObjectForEntityForName을 다시 호출하면 코어 데이터에 이미 존재하는 경우에도 매번 새로운 관리 대상 개체가 삽입됩니다.

새로운 개체를 추가해야하는지 확인하고 확인할 필요가있을 때마다 하나의 개체를 가져 오는 것이 아주 느립니다. 현재로드 한 객체 (또는 고유 한 식별 필드)를 NSArray 또는 NSSet에 캐시하여 멤버쉽을 확인할 수 있습니다.

관련 문제