코어 데이터의 내용으로 UITableView를 업데이트하는 NSFetchedResultsController가 있습니다. 그것은 꽤 표준적인 물건들입니다. 그러나 나는 당신이 모두 여러 번 보았을 것이라고 확신합니다. 그러나 나는 약간의 문제에 직면하고 있습니다. 첫 번째 코드는 다음과 같습니다.NSFetchedResultsController는 fetchLimit를 무시합니까?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Article" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchLimit:20];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(folder.hidden == NO)"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"sortDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort1, nil]];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[fetchRequest release];
controller.delegate = self;
self.fetchedResultsController = controller;
[controller release];
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
// TODO send error notification
NSLog(@"%@", [error localizedDescription]);
}
문제는 처음에는 웹 서비스에서 다운로드하고 동기화 할 때 엔티티가 없습니다. NSFetchedResultsController는 테이블에서 150 개가 넘는 엔티티 행 (웹 서비스가 반환하는 항목 수)을 채 웁니다. 그러나 나는 그것을 무시하고있는 것처럼 보이는 20의 반입 제한을 설정 중입니다. 그러나 앱을 닫고 이미 상점에있는 데이터로 다시 시작하면 정상적으로 작동합니다. 내 대리자를 임 내가 이렇게 : 어떤 아이디어가 무엇
애플의 dev에 문서에서 복사 - 붙여 넣기 꽤 많이#pragma mark -
#pragma mark NSFetchedResultsControllerDelegate methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
을에 간다?
좋은 소리 나는 그에게 총을 내고 무슨 일이 일어나는 지 알려줄 것입니다. – marchinram
나는 이것이 필수적이라고 확신하지 않는다 ... 그것이 효과가 있었느냐? –
사람들은 왜 아직도 abort()를했는지 궁금합니다. 코드에서. 응용 프로그램이 거부됩니다 ;-) 적절한 오류 처리만으로도 충분합니다 (또는 NSAssert 만 사용하십시오) – Lukasz