NSFetchedResultsController/NSFetchRequest 与 NSManagedObject 的多对多关系
我在 iOS 上展示了一个带有不同视图控制器的 NSManagedObject
层次结构(基本上是三个级别)。我不完全理解的问题是每个视图控制器是否都需要 NSFetchedResultsController
。对于根视图控制器来说,这似乎很方便,但是后面的视图基于前一个视图中的选择。与直接关系相比,NSFetchRequest
怎么样?显示的数据是静态的,永远不会改变,因此无需准备更新。它将从核心数据 sqlite 文件加载。
这是我所拥有的简单图形:
A (1) <---> (*) B (1) <---> (*) C
因此 A
-B
和 B
-C
关系都是一-一对多,在根视图中我显示了 A
列表。选择一个 A
将在下一个视图控制器中显示它的 B
,依此类推。
无论我使用 NSFetchRequest
(或 NSFetchedResultsController
)获取 B
的实例还是简单地使用 B
中的一对多关系,有什么区别吗?代码>A?内存占用或内存不足的情况下有什么区别吗?我知道我总是可以在 didReceiveMemoryWarning
时放弃获取控制器,但对于托管对象,我认为事情没那么简单。是这样吗?我很想利用直接关系。
提前致谢。
I am presenting an NSManagedObject
hierarchy (basically three levels) with different view controllers on iOS. The issue I don't completely understand is that if there is need for NSFetchedResultsController
for each of the view controllers. For the root view controller it seems convenient but the views that are following this are based on the selection in the previous view. What about NSFetchRequest
when compared to direct relationships? The data being displayed is static and it will not change ever, so there is no need to prepare for updates. It will be loaded from a core data sqlite file.
Here's the simple graphic of what I have:
A (1) <---> (*) B (1) <---> (*) C
So both A
-B
and B
-C
relationships are one-to-many and in the root view I'm displaying a list of A
s. Selecting one A
will display it's B
s in the next view controller and so forth.
Is there any difference whether I'm fetching the instances of B
with NSFetchRequest
(or NSFetchedResultsController
) or simply using the to-many relationship in A
? Is there any difference in memory footprint or under situations when memory is running low? I understand that I can always give the fetch controller away when didReceiveMemoryWarning
, but with managed objects I think it's not that simple. Is this so? I'd love to use the direct relationships.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是有区别的,但你不必担心,除非每个 A 都有数千个 B 和 C。
如果你使用这种关系,那么你会得到一个
NSSet
返回 - 这不会使用NSFetchedResultsController
。但是,您返回的对象实际上不会包含任何数据,它们都会以错误开始,因此内存占用非常小。他们只会在您要求时检索数据:)并且如果您收到内存不足警告,您可以通过从上下文重新加载托管对象来强制它们再次成为故障(请参阅此处的文档)。
There's a difference but not one you should worry about unless you have thousands of Bs and Cs for each A.
If you use the relationship then you get an
NSSet
back - this won't useNSFetchedResultsController
. However, the objects you get back won't actually have any data in, they will all start out as faults so the memory footprint is pretty small. They only retrieve their data once you ask for it :)And if you get a low memory warning you can force managed objects to become faults again by reloading them from the context (see the docs here).