CoreData 從入門到精通(五)CoreData 和 TableView 結(jié)合

我們知道 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 指定之后就不可以再修改了藐窄;

fetchRequest-w600

context 就是上下文的對(duì)象资昧;sectionNameKeyPath 可以指定一個(gè) keypath 來為 tableView 生成不同的 section,指定成 nil 的話荆忍,就只生成一個(gè) section格带;

sectionNameKeyPath-w600

cacheName 用來指定一個(gè)緩存的名字,加載好的數(shù)據(jù)會(huì)緩存到這樣一個(gè)私有的文件夾里刹枉,這樣可以避免過多的從 CoreData 數(shù)據(jù)庫里查詢以及計(jì)算的操作叽唱。

cacheName-w600

除此之外,fetchLimitfetchBatchSize 這兩個(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ù)保存在 fetchedResultsControllerfetchedObjects 屬性里钉稍,不過我們一般不會(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) fetchedResultsControllerdelegate 就可以了消恍。

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é)合是不是很方便呢俩檬。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末萎胰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子棚辽,更是在濱河造成了極大的恐慌技竟,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,378評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屈藐,死亡現(xiàn)場(chǎng)離奇詭異灵奖,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)估盘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門瓷患,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人遣妥,你說我怎么就攤上這事擅编。” “怎么了箫踩?”我有些...
    開封第一講書人閱讀 168,983評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵爱态,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我境钟,道長(zhǎng)锦担,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評(píng)論 1 299
  • 正文 為了忘掉前任慨削,我火速辦了婚禮洞渔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘缚态。我一直安慰自己磁椒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評(píng)論 6 398
  • 文/花漫 我一把揭開白布玫芦。 她就那樣靜靜地躺著浆熔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪桥帆。 梳的紋絲不亂的頭發(fā)上医增,一...
    開封第一講書人閱讀 52,549評(píng)論 1 312
  • 那天慎皱,我揣著相機(jī)與錄音,去河邊找鬼叶骨。 笑死茫多,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的邓萨。 我是一名探鬼主播地梨,決...
    沈念sama閱讀 41,063評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼菊卷,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼缔恳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起洁闰,我...
    開封第一講書人閱讀 39,991評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤歉甚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后扑眉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纸泄,經(jīng)...
    沈念sama閱讀 46,522評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評(píng)論 3 342
  • 正文 我和宋清朗相戀三年腰素,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了聘裁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,742評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡弓千,死狀恐怖衡便,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情洋访,我是刑警寧澤镣陕,帶...
    沈念sama閱讀 36,413評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站姻政,受9級(jí)特大地震影響呆抑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜汁展,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評(píng)論 3 335
  • 文/蒙蒙 一鹊碍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧食绿,春花似錦妹萨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至品洛,卻和暖如春树姨,著一層夾襖步出監(jiān)牢的瞬間摩桶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工帽揪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留硝清,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,159評(píng)論 3 378
  • 正文 我出身青樓转晰,卻偏偏與公主長(zhǎng)得像芦拿,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子查邢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,166評(píng)論 30 470
  • 你可以沒有腦子 但至少吃一塹長(zhǎng)一智 你可以不漂亮 但至少身邊要有幾個(gè)朋友 你可以圈子不大 但至少圈子請(qǐng)干凈 你可以...
    奶味蘿莉閱讀 146評(píng)論 0 0
  • 春天的一生痛苦 他一生幸福
    fup閱讀 187評(píng)論 0 0