당신의 어플리케이션 델리게이트는 테이블 뷰 컨트롤러에 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 class 및 NSFetchedResultsControllerDelegate protocol 참조를 모두 읽으십시오. Apple의 설명서에는 완전한 소스 코드 예제가 포함되어 있습니다.
아 좋아! 이것을 시험해보십시오! 고맙습니다! – JAManfredi