我应该在每个视图中都有NSFetchedResultsController吗?

时间:2021-09-20 20:10:01

I'm using Core Data in my first iPhone application and trying to understand NSFetchedResultsController. It works great in my root view. Do I need to instantiate an instance in every one of my view controllers? If so, is there a template that makes this as easy as it was in my root controller (I just checked a box in the template when creating the project). When I add a single new view controller I don't see an option to use Core Data.

我在我的第一个iPhone应用程序中使用Core Data并尝试了解NSFetchedResultsController。它在我的根视图中工作得很好。我是否需要在每个视图控制器中实例化一个实例?如果是这样,是否有一个模板可以让它像我的根控制器一样简单(我在创建项目时只是在模板中选中了一个框)。当我添加一个新的视图控制器时,我没有看到使用Core Data的选项。

Update: Even after I did cut/paste the code into my second view, it took me a while to realize that I also needed to set the managedObjectContext before switching to the new view. I added the following line to my RootViewController before pushing the new view on the navigation stack:

更新:即使我将代码剪切/粘贴到第二个视图中,我花了一段时间才意识到我还需要在切换到新视图之前设置managedObjectContext。在推送导航堆栈上的新视图之前,我将以下行添加到我的RootViewController:

self.newVC.managedObjectContext = self.managedObjectContext;

2 个解决方案

#1


11  

If your other views are visualizing different Entities, then yes, you would use a different NSFetchedResultsController. You can basically get away with copy-and-pasting the code from the autogenerated root view controller for your other view controllers... just change the Entity name.

如果您的其他视图可视化不同的实体,那么是的,您将使用不同的NSFetchedResultsController。基本上可以通过自动生成的根视图控制器为其他视图控制器复制和粘贴代码...只需更改实体名称即可。

However, if the other (table) views down your hierarchy are just displaying different attributes of the same Entity, it's more efficient/simpler to just pass the existing NSFetchedResultsController object down the hierarchy. Just create a NSFetchedResultsController member in the class interface and expose it as a property in the view controller's .h file, and then synthesize the property and release it in its .m file. Then set the property before you push the view controller on the stack.

但是,如果您的层次结构中的另一个(表)视图仅显示同一实体的不同属性,那么将现有NSFetchedResultsController对象传递到层次结构中会更有效/更简单。只需在类接口中创建一个NSFetchedResultsController成员,并将其作为视图控制器的.h文件中的属性公开,然后合成该属性并将其释放到其.m文件中。然后在将视图控制器推入堆栈之前设置该属性。

#2


3  

I'd like to add that if you are using multiple NSFetchedResultsControllers for the same entity, but in different tables, your UITableView will not update if you insert data for the same entity using another controller. This is because your UITableView will not receive willChangeContent ,etc messages from an NSFetchedResultsController for which you have not set yourself as a delegate.

我想补充一点,如果您对同一个实体使用多个NSFetchedResultsControllers,但在不同的表中,如果使用另一个控制器为同一实体插入数据,则UITableView将不会更新。这是因为您的UITableView不会从NSFetchedResultsController接收到您没有将自己设置为委托的willChangeContent等消息。

#1


11  

If your other views are visualizing different Entities, then yes, you would use a different NSFetchedResultsController. You can basically get away with copy-and-pasting the code from the autogenerated root view controller for your other view controllers... just change the Entity name.

如果您的其他视图可视化不同的实体,那么是的,您将使用不同的NSFetchedResultsController。基本上可以通过自动生成的根视图控制器为其他视图控制器复制和粘贴代码...只需更改实体名称即可。

However, if the other (table) views down your hierarchy are just displaying different attributes of the same Entity, it's more efficient/simpler to just pass the existing NSFetchedResultsController object down the hierarchy. Just create a NSFetchedResultsController member in the class interface and expose it as a property in the view controller's .h file, and then synthesize the property and release it in its .m file. Then set the property before you push the view controller on the stack.

但是,如果您的层次结构中的另一个(表)视图仅显示同一实体的不同属性,那么将现有NSFetchedResultsController对象传递到层次结构中会更有效/更简单。只需在类接口中创建一个NSFetchedResultsController成员,并将其作为视图控制器的.h文件中的属性公开,然后合成该属性并将其释放到其.m文件中。然后在将视图控制器推入堆栈之前设置该属性。

#2


3  

I'd like to add that if you are using multiple NSFetchedResultsControllers for the same entity, but in different tables, your UITableView will not update if you insert data for the same entity using another controller. This is because your UITableView will not receive willChangeContent ,etc messages from an NSFetchedResultsController for which you have not set yourself as a delegate.

我想补充一点,如果您对同一个实体使用多个NSFetchedResultsControllers,但在不同的表中,如果使用另一个控制器为同一实体插入数据,则UITableView将不会更新。这是因为您的UITableView不会从NSFetchedResultsController接收到您没有将自己设置为委托的willChangeContent等消息。