IOS --- NSFetchResultsController
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];}