NSFetchedResultsController和UITableView集成起来处理数据具有强大的灵活性。首先得到的好处是不需要将数据记录进行分页,不然,按照传统的做法,需要先查询出总的记录,然后再从纪录里面过滤,这样会进行两次操作,对内存消耗很大,处理不好,程序甚至可能崩溃。使用NSFetchedResultsController类不仅简单,还具有更高的性能,这个类自动帮助你记录分页的事情,得到表对应的Core Data对象也非常简单。
更重要的是,你在其他界面更新或者删除记录时,NSFetchedResultsController可以帮助你同步更新UITableView,你的UITableView和数据库同步将变得非常简单。
以下是实现NSFetchedResultsController的委托方法
/*
*NSFetchResultsController类是将一个获取请求和一个上下文作为其输入并在获取请求中的数据改变时调用该类的委托方法
*/
- (NSFetchedResultsController *)fetchedResultsController
{
NSLog(@"fetchedResultsController");
//检测是否已经创建了fetchedResultsController
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
/*
*你需要一个获取请求和一个上下文以能够使用fetchedResultsController
*/
//你可以将获取请求视作SQL SELECT语句
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//基于上下文中的Event实体创建一个实体
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
//设置该实体由fetchRequest使用
[fetchRequest setEntity:entity];
//设置fetchRequest的批大小为单词接收的合理记录数量
[fetchRequest setFetchBatchSize:20];
//创建一个NSSortDescriptor,使用NSSortDescriptor对fetchRequest的结果排序(基于“timeStamp”字段降序排序)
//(你可以将NSSortDescriptor视为SQL ORDER BY字句)
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
//需要一个获取请求和一个上下文以能够使用fetchedResultsController
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;
}
//方法通知你“获取结果控制器”将更改内同
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"controllerWillChangeContent");
//通过调用表示图的beginUpdates方法告知表更新即将发生
[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;
default:
return;
}
}
- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
//通知你“获取结果控制器”完成了其更改
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"controllerDidChangeContent");
//通过调用endUpdates方法告知表视图更新结束
[self.tableView endUpdates];
}
时间: 2024-10-10 04:17:17