zoukankan      html  css  js  c++  java
  • Core Data 教程: 如何使用NSFetchedResultsController

    为什么要采用NSFetchedResultsController?

    迄今为止,我们现在的处境跟当初用SQLite3的时候一样。然而,我们不需要写如此多的代码(注意一下FailedBankDatabase类中那段缺失的原始SQL语句代码),添加诸如插入/删除之类的操作也很简便。

    有一项显著的便利性是用Core Data才能体现出来的:使用NSFetchedResultsController。

    比较理想的状态是在用户目前所见的table view中加载部分行。幸运的是,苹果为我们造就了这一便利,提供了一个很棒的工具类:NSFetchedResultsController。

    因此,开始打开FailedBanksListViewController.h,移除旧有的NSArray failedBankInfos,替换为一个新的NSFetchedResultsController:

    @interface FailedBanksListViewController : UITableViewController
        <NSFetchedResultsControllerDelegate> {
        NSFetchedResultsController *_fetchedResultsController;
        NSManagedObjectContext *_context;
    }
    
    @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
    @property (nonatomic, retain) NSManagedObjectContext *context;
    
    @end

    在synthesize段移除老的failedBankInfos代码并添加:

    @synthesize fetchedResultsController = _fetchedResultsController;

    以防忘记,在dealloc方法中fetchedResultsController设为nil

    self.fetchedResultsController = nil;

    另一个绝妙的地方就是你可以在viewDidUnload里面将NSFetchedResultsController设为nil,这就意味着在低内存情况下,内存的所有数据都可以释放掉(在视图离开视线的情况下)。你只需要在viewDidUnload里面把它清空(并保证在viewDidLaod中重新初始化)

    - (void)viewDidUnload {
        self.fetchedResultsController = nil;
    }
  • 相关阅读:
    React简明学习
    react-router简明学习
    react组件生命周期
    在vue中使用css modules替代scroped
    深入理解javascript中的事件循环event-loop
    javascript中的内存管理和垃圾回收
    移动端中的陀螺仪
    基于create-react-app的再配置
    vscode常用设置
    更高效地使用搜索引擎
  • 原文地址:https://www.cnblogs.com/greywolf/p/2807436.html
Copyright © 2011-2022 走看看