2011-12-28 2 views
1

iPhone 프로그래밍과 새로운 기능, CoreData. 나는 일을하는 몇 가지 일반적인 관행과 방법에 대해 궁금해하고있었습니다.핵심 데이터 및 UITableView 프랙티스/질문

CoreData의 엔티티를 UITableView에 추가 /로드하려고합니다. 그래서 AppDelegate에서 didfinishlaunchingwithoptions에 NSArray 엔티티 (NSManagedObjects)를로드하고 NSArray를 테이블 뷰 컨트롤러로 채 웁니다. 테이블 뷰 컨트롤러에서는 NSManagedObjects의 NSArray를 사용하여 cellForRowAtIndexPath 메서드에서 셀 뷰를 지정합니다.

이렇게하는 것이 가장 좋은 방법입니까? NSManagedObjects 배열을 사용하여이로드 및 추가/삭제 배열을 관리해야합니까? 가져 오기를 반복하고 각 셀에 포함 할 각 개체를 나타 내기 위해 별도로 만든 새 클래스 개체를 채워야합니까?

나는 필요한 것보다 더 많은 일을하고 싶지 않지만, 아무것도 못하게한다.

도와 주셔서 감사합니다.

답변

8

당신의 어플리케이션 델리게이트는 테이블 뷰 컨트롤러에 NSManagedObjectContext을 전달하면 UITableView에 표시 할 관리 객체를 효율적으로로드하고 객체 그래프의 변경 사항에 응답하는 클래스 인 NSFetchedResultsController이 생성됩니다.

Xcode 4.2의 "마스터 - 세부 응용 프로그램"핵심 데이터 프로젝트 템플릿은이 패턴을 사용합니다. 좋은 참고서이며 출발점입니다. 당신이 NSFetchedResultsController이 있으면

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (__fetchedResultsController != nil) { 
     return __fetchedResultsController; 
    } 

    // Set up the fetched results controller. 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:20]; 

    // Edit the sort key as appropriate. 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; 
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 

    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&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(); 
    } 

    return __fetchedResultsController; 
} 

, 예를 들어, 테이블 뷰 데이터 소스 방법으로 인출 된 객체를 연결 단지 문제 : 마스터 테이블 뷰 컨트롤러가 느리게로드하고 그 결과 컨트롤러를 구성하는 방법 여기입니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = <#Get the cell#>; 
    NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath]; 
    // Configure the cell with data from the managed object. 
    return cell; 
} 

자세한 내용은 프로젝트 템플릿을보고 NSFetchedResultsController classNSFetchedResultsControllerDelegate protocol 참조를 모두 읽으십시오. Apple의 설명서에는 완전한 소스 코드 예제가 포함되어 있습니다.

+0

아 좋아! 이것을 시험해보십시오! 고맙습니다! – JAManfredi