我应该在每个视图中都有一个NSFetchedResultsController? [英] Should I have a NSFetchedResultsController in every view?

查看:89
本文介绍了我应该在每个视图中都有一个NSFetchedResultsController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的第一个iPhone应用程序中使用Core Data,并试图理解NSFetchedResultsController。它在我的根视图中工作伟大。我需要在每个我的视图控制器中实例化一个实例吗?如果是这样,是否有一个模板,使这很容易,因为它是在我的根控制器(我刚刚在模板中创建项目时选中一个框)。当我添加一个单一的新的视图控制器时,我没有看到使用Core Data的选项。

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.

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

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:

self.newVC.managedObjectContext = self.managedObjectContext;


推荐答案

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

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.

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

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆