我們知道 CoreData 里存儲(chǔ)的是具有相同結(jié)構(gòu)的一系列數(shù)據(jù)的集合蹦狂,TableView 正好是用列表來展示一系列具有相同結(jié)構(gòu)的數(shù)據(jù)集合的术健。所以焕蹄,要是 CoreData 和 TableView 能結(jié)合起來酬蹋,CoreData 查詢出來的數(shù)據(jù)能同步地顯示在 TableView 上身笤,更好一點(diǎn)就是 CoreData 里的改動(dòng)也能同步到 TableView 上糖声,那就再好不過了斤彼。可喜的是蘸泻,確實(shí)有這樣一個(gè) API琉苇,那就是 NSFetchedResultsController
,相信不少人對(duì)這個(gè)東西都不陌生悦施,因?yàn)橛?Xcode 創(chuàng)建帶有 CoreData 的 Master-Detail 模板工程時(shí)并扇,就是用這個(gè)接口來實(shí)現(xiàn)的。這篇文章也主要是圍繞著模板工程中的代碼進(jìn)行介紹抡诞,如果你對(duì)這塊比較熟悉的話穷蛹,不妨直接去看模板里的代碼;如果你是第一次聽說這個(gè) API昼汗,不妨繼續(xù)看下去肴熏,相信會(huì)對(duì)你有幫助的。
創(chuàng)建一個(gè)簡(jiǎn)單的 TableView 布局
在使用 CoreData 之前顷窒,首先我們來創(chuàng)建一個(gè)簡(jiǎn)單的 TableView 布局蛙吏,對(duì)大多數(shù)人來說,這應(yīng)該沒什么難度鞋吉,所以下面就直接貼代碼鸦做,不會(huì)對(duì)代碼進(jìn)行解釋了。
這里我們用 Storyboard 創(chuàng)建一個(gè) TableViewController谓着,首先配置 tableView 的 dataSource
- (void)viewDidLoad {
[super viewDidLoad];
// 添加編輯和插入按鈕
self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem = [self addBarButtonItem];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// deletions
}
}
這是一個(gè)非常簡(jiǎn)單的 TableView泼诱,現(xiàn)在只能顯示一些隨機(jī)生成的數(shù)據(jù)。接下來我們來實(shí)現(xiàn) NSFetchedResultsController
來和 CoreData 中的數(shù)據(jù)對(duì)接漆魔,CoreData 相關(guān)的代碼坷檩,就直接用之前文章里創(chuàng)建的 Student 實(shí)體。如果有不了解的朋友改抡,可以先去看一下這篇文章 CoreData 從入門到精通 (一) 數(shù)據(jù)模型 + CoreData 棧的創(chuàng)建矢炼。
初始化 NSFetchedResultsController
先來看一下 NSFetchedResultsController
的初始化代碼:
#pragma mark - NSFetchedResultsController
- (NSFetchedResultsController<Student *> *)fetchedResultsController {
if (!_fetchedResultsController) {
NSFetchRequest *fetchRequest = [Student fetchRequest];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"studentId" ascending:YES]];
fetchRequest.fetchBatchSize = 50;
fetchRequest.fetchLimit = 200;
NSFetchedResultsController *fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:@"studentAge" cacheName:@"StudentTable"];
fetchController.delegate = self;
NSError *error;
[fetchController performFetch:&error];
[[[Logger alloc] init] dealWithError:error whenFail:@"fetch failed" whenSuccess:@"fetch success"];
_fetchedResultsController = fetchController;
}
return _fetchedResultsController;
}
創(chuàng)建 fetchedResultsController
需要指定一個(gè) fetchRequest
,這很好理解阿纤,因?yàn)?fetchedResultsController
也需要查詢 CoreData 數(shù)據(jù)庫里的數(shù)據(jù)句灌,需要注意的是,指定的這個(gè) fetchRequest
必須要設(shè)置 sortDescriptors
也就是排序規(guī)則這個(gè)屬性欠拾,不設(shè)置直接運(yùn)行的話胰锌,程序是會(huì)直接崩潰的,這是因?yàn)?fetchedResultsController
需要根據(jù)這個(gè)排序規(guī)則來規(guī)定數(shù)據(jù)該以什么順序顯示到 tableView 上,而且這個(gè) fetchRequest
指定之后就不可以再修改了藐窄;
context
就是上下文的對(duì)象资昧;sectionNameKeyPath
可以指定一個(gè) keypath 來為 tableView 生成不同的 section,指定成 nil 的話荆忍,就只生成一個(gè) section格带;
cacheName
用來指定一個(gè)緩存的名字,加載好的數(shù)據(jù)會(huì)緩存到這樣一個(gè)私有的文件夾里刹枉,這樣可以避免過多的從 CoreData 數(shù)據(jù)庫里查詢以及計(jì)算的操作叽唱。
除此之外,fetchLimit
和 fetchBatchSize
這兩個(gè)屬性也需要注意一下微宝,fetchLimit
之前講過是指定獲取數(shù)據(jù)的上限數(shù)量棺亭,而 fetchBatchSize
是分批查詢的數(shù)據(jù)量大小。因?yàn)橐淮涡圆樵兂鲞^多的數(shù)據(jù)會(huì)消耗不少的內(nèi)存蟋软,所以這里推薦給這兩個(gè)屬性設(shè)置一個(gè)合理的值镶摘;指定的泛型 Student
就是 fetchRequest
查詢的數(shù)據(jù)類型。這些都配置之后調(diào)用 performFetch:
方法就可以執(zhí)行查詢操作了岳守。返回的數(shù)據(jù)保存在 fetchedResultsController
的 fetchedObjects
屬性里钉稍,不過我們一般不會(huì)直接用到它。
fetchedResultsController 綁定到 TableView
下面來修改 tableView 的 dataSource
的方法將查詢出來的數(shù)據(jù)集合和 TableView 綁定棺耍。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// sections 是一個(gè) NSFetchedResultsSectionInfo 協(xié)議類型的數(shù)組贡未,保存著所有 section 的信息
return self.fetchedResultsController.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// sectionInfo 里的 numberOfObjects 屬性表示對(duì)應(yīng) section 里的結(jié)果數(shù)量
id<NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return sectionInfo.numberOfObjects;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
// 通過這個(gè)方法可以直接獲取到對(duì)應(yīng) indexPath 的實(shí)體類對(duì)象
Student *student = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self configureCell:cell withStudent:student];
return cell;
}
// 修改后的configureCell 方法
- (void)configureCell:(UITableViewCell *)cell withStudent:(Student *)student {
cell.textLabel.text = [NSString stringWithFormat:@"%d:%@ age:%d", student.studentId, student.studentName, student.studentAge];
}
實(shí)現(xiàn)增刪改查的同步更新
上一步里我們實(shí)現(xiàn)了把 fetchedResultsController
里的數(shù)據(jù)綁定到 TableView 上,但還沒完成同步更新的實(shí)現(xiàn)蒙袍,例如 CoreData 數(shù)據(jù)庫里新插入了數(shù)據(jù)俊卤,TableView 這時(shí)也可以自動(dòng)更新。實(shí)現(xiàn)這個(gè)功能害幅,只需要實(shí)現(xiàn) fetchedResultsController
的 delegate
就可以了消恍。
NSFetchedResultsControllerDelegate
里有一個(gè) NSFetchedResultsChangeType
枚舉類型,其中的四個(gè)成員分別對(duì)應(yīng) CoreData 里的增刪改查:
typedef NS_ENUM(NSUInteger, NSFetchedResultsChangeType) {
NSFetchedResultsChangeInsert = 1,
NSFetchedResultsChangeDelete = 2,
NSFetchedResultsChangeMove = 3,
NSFetchedResultsChangeUpdate = 4
}
delegate
里共有五個(gè)協(xié)議方法:
// 對(duì)應(yīng) indexPath 的數(shù)據(jù)發(fā)生變化時(shí)會(huì)回調(diào)這個(gè)方法
@optional
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(nullable NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(nullable NSIndexPath *)newIndexPath;
// section 發(fā)生變化時(shí)會(huì)回調(diào)這個(gè)方法
@optional
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type;
// 數(shù)據(jù)內(nèi)容將要發(fā)生變化時(shí)會(huì)回調(diào)
@optional
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller;
// 數(shù)據(jù)內(nèi)容發(fā)生變化之后會(huì)回調(diào)
@optional
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller;
// 返回對(duì)應(yīng) section 的標(biāo)題
@optional
- (nullable NSString *)controller:(NSFetchedResultsController *)controller sectionIndexTitleForSectionName:(NSString *)sectionName以现;
@end
想要實(shí)現(xiàn) tableView 的數(shù)據(jù)同步更新可以按下面的代碼來實(shí)現(xiàn)這幾個(gè) delegate 方法:
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// 在這里調(diào)用 beginUpdates 通知 tableView 開始更新狠怨,注意要和 endUpdates 聯(lián)用
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
// beginUpdates 之后约啊,這個(gè)方法會(huì)調(diào)用,根據(jù)不同類型佣赖,來對(duì)tableView進(jìn)行操作恰矩,注意什么時(shí)候該用 indexPath,什么時(shí)候用 newIndexPath.
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeMove:
[self.tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] withStudent:anObject];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// 更新完后會(huì)回調(diào)這里憎蛤,調(diào)用 tableView 的 endUpdates.
[self.tableView endUpdates];
}
最后來實(shí)現(xiàn)一開始添加的編輯和插入按鈕的操作:
- (UIBarButtonItem *)addBarButtonItem {
UIBarButtonItem *addBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addRandomStudent)];
return addBarButtonItem;
}
// 為了看到插入效果外傅,可以把 fetchedResultsController 的fetchLimit 和 fetchBatchSize 調(diào)小一些.
- (void)addRandomStudent {
NSString *name = [NSString stringWithFormat:@"student-%u", arc4random_uniform(100000)];
int16_t age = (int16_t)arc4random_uniform(10) + 10;
int16_t stuId = (int16_t)arc4random_uniform(INT16_MAX);
Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
student.studentName = name;
student.studentAge = age;
student.studentId = stuId;
[self.context save:nil];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Student *student = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.context deleteObject:student];
[self.context save:nil];
}
}
到此為止 NSFetchedResultsController
的使用就講完了,有了它 CoreData 和 TableView的結(jié)合是不是很方便呢俩檬。