使用 NSFetchedResultController 显示DetailsViewController
在我的 iPhone 应用程序中,我有多个关系主题的核心数据 -->>问题,所以每个主题都包含几个问题,我想出了如何使用 NSFetchedResultController 在 UItableView 中显示主题列表,但我不知道如何构建 FetchedResultController 来显示所选主题中的问题。
我试图在互联网上找到类似的教程或示例,但找不到任何东西。
我该怎么做呢?
非常感谢
In my iphone app I have core data to-many relation topic -->> question, so every topic contains a few questions, I figured out how to display list of topics in UItableView using NSFetchedResultController, but I can't get how shall I construct my FetchedResultController to display questions in chosen topic.
I'm trying to find on Internet similar tutorial or example but can't find anything.
How can I do it?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要需要返回到
NSFetchRequest
来获取关系另一端的对象。只需向您的主题
询问问题即可。这将为您提供一个包含故障实体的NSSet
。有故障的实体占用非常很少的内存,您可以轻松地提取其中的 1000 个。如果您需要过滤这些内容,那么您可以对返回的
NSSet
执行-filteredSetWithPredicate:
,但写入磁盘既浪费又缓慢。You do NOT need to go back to a
NSFetchRequest
to get the objects on the other end of a relationship. Just ask yourTopic
for its questions. This will give you aNSSet
of entities that are faulted. Faulted entities take up VERY little memory and you can pull in 1000 of them easily.If you need to filter those then you can perform a
-filteredSetWithPredicate:
on theNSSet
that is returned but going out to disk is wasteful and slow.您不会使用 NSFetchedResultsController。相反,您将在详细信息视图控制器中定义一个属性。
然后,当您创建详细信息视图控制器时,您将根据 didSelectRowForIndexPath 中选择的主题设置该属性。
然后在详细视图中,只需使用核心数据关系(topic.questions)从 Topic 对象中获取问题即可
You won't use an NSFetchedResultsController. Instead you'll define a property in your detail view controller
Then when you create your detail view controller, you'll set the property based on the topic selected in didSelectRowForIndexPath.
Then in your detail view it's just a matter of grabbing the questions out of the Topic object using the core data relationship (topic.questions)
好吧,我发现我需要使用
NSPredicate
来实现它。现在我尝试将其添加到我的 fetchedResultController 中,如下所示:但出现错误:
我该怎么做?
谢谢
编辑
明白了!
需要在数据模型中设置双方关系并设置谓词,如下所示:
[NSPredicate predicateWithFormat:@"topic.topicName == %@", self.topic]
Ok, I found out that I need to use
NSPredicate
for it. Now I'm trying to add it to my fetchedResultController like this:but getting an error:
How can I do it?
Thanks
Edit
Got it!
need to set up both side relation in data model and set predicate like this:
[NSPredicate predicateWithFormat:@"topic.topicName == %@", self.topic]