標籤:
應用場合:配合Core Data和Table View使用,Core Data儲存資料,Table View顯示資料
1.在需要顯示資料的視圖控制器(包含有table view)中添加NSFetchedResultsController執行個體變數
@implementation LocationsViewController{
NSFetchedResultsController *_fetchedResultsController;
}
2.惰性執行個體化NSFetchedResultsController執行個體變數
-(NSFetchedResultsController *)fetchedResultsController
{
if(_fetchedResultsController == nil)
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; //建立一個請求
//實體描述
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//設定排序
NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"category" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor1,sortDescriptor2]];
//每次擷取20個資料
[fetchRequest setFetchBatchSize:20];
//按“category”關鍵字分組,cacheName參數的名字用於唯一標記擷取的資料結果,fetchedResultsController會將這些資料緩衝起來
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"category" cacheName:@"Locations"];
_fetchedResultsController.delegate = self;
}
return _fetchedResultsController;
}
3.讓視圖控制器實現NSFetchedResultsController協議
@interface LocationsViewController () <NSFetchedResultsControllerDelegate>
@end
4.調用方法開始擷取Core Data儲存的資料
- (void)viewDidLoad {
[super viewDidLoad];
[self performFetch];
}
-(void)performFetch
{
NSError *error;
if(![self.fetchedResultsController performFetch:&error])
{
//添加處理錯誤情況的代碼
return;
}
}
5.添加以下代碼
- (void)dealloc {
_fetchedResultsController.delegate = nil;
}
6.實現table view資料來源返回行數的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
7.擷取Core Data儲存的某一個資料(實體)時用以下語句,之後便可對該對象進行刪除,修改等操作,然後再同步到Core Data資料(參考Core Data博文)
Location *location = [self.fetchedResultsController objectAtIndexPath:indexPath];
8.實現NSFetchedResultsController協議方法
#pragma mark - NSFetchedResultsControllerDelegate
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"*** controllerWillChangeContent");
[self.tableView beginUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(nonnull id)anObject atIndexPath:(nullable NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(nullable NSIndexPath *)newIndexPath
{
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"*** NSFetchedResultsChangeInsert (object)");
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
NSLog(@"*** NSFetchedResultsChangeDelete (object)");
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
NSLog(@"*** NSFetchedResultsChangeUpdate (object)");
//添加調用設定table view cell內容的方法,該方法需要另行建立
break;
case NSFetchedResultsChangeMove:
NSLog(@"*** NSFetchedResultsChangeMove (object)");
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(nonnull id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"*** NSFetchedResultsChangeInsert (section)");
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
NSLog(@"*** NSFetchedResultsChangeDelete (section)");
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove: break;
case NSFetchedResultsChangeUpdate: break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"*** controllerDidChangeContent");
[self.tableView endUpdates];
}
9.實現以下方法,當滑動刪除cell時,同時同步到Core Data資料庫
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
Location *location = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:location];
NSError *error;
if(![self.managedObjectContext save:&error])
{
//添加處理錯誤情況的代碼
return;
}
}
}
10.實現table view分組相關的協議方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo name];
}
iOS開發學習之NSFetchedResultsController