IOS --- NSFetchResultsController

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.