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 )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];
}