使用 NSFetchedResultController 显示DetailsViewController

发布于 2024-09-14 18:08:19 字数 234 浏览 6 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-09-21 18:08:19

不需要需要返回到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 your Topic for its questions. This will give you a NSSet 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 the NSSet that is returned but going out to disk is wasteful and slow.

缺⑴份安定 2024-09-21 18:08:19

您不会使用 NSFetchedResultsController。相反,您将在详细信息视图控制器中定义一个属性。

@property (nonatomic, retain) Topic *topic;

然后,当您创建详细信息视图控制器时,您将根据 didSelectRowForIndexPath 中选择的主题设置该属性。

然后在详细视图中,只需使用核心数据关系(topic.questions)从 Topic 对象中获取问题即可

You won't use an NSFetchedResultsController. Instead you'll define a property in your detail view controller

@property (nonatomic, retain) Topic *topic;

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)

誰認得朕 2024-09-21 18:08:19

好吧,我发现我需要使用 NSPredicate 来实现它。现在我尝试将其添加到我的 fetchedResultController 中,如下所示:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topic == %@", self.topic];
[fetchRequest setPredicate:predicate];

但出现错误:

'keypath topic not found in entity <NSSQLEntity QuestionItem id=2>'

我该怎么做?

谢谢

编辑

明白了!

需要在数据模型中设置双方关系并设置谓词,如下所示:

[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:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topic == %@", self.topic];
[fetchRequest setPredicate:predicate];

but getting an error:

'keypath topic not found in entity <NSSQLEntity QuestionItem id=2>'

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文