获取与核心数据的关系

发布于 2024-08-18 21:43:34 字数 936 浏览 10 评论 0原文

我有两个具有双向关系的托管对象。 它是一个包含“segment”对象的“segmentWithDetails”。

我用来

NSEntityDescription *entity = [NSEntityDescription entityForName:@"SegmentWithDetails" 
                                          inManagedObjectContext:connectionDetailsService.connectionDetailsContext];
[fetchRequest setEntity:entity];

获取我的细分详细信息。这工作正常。

但是,当我尝试检索包含的段时,我得到了零。

我试图用

Segment *segment = [segmentWithDetails valueForKeyPath:@"segment"];

和 with 来

Segment *segment = segmentWithDetails.segment;

获取它但这不起作用(=nil)。我还尝试修改 NSFetchedResultsController。 我添加了以下内容:

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"segment"]];
[fetchRequest setIncludesSubentities:YES];

但这两者似乎没有任何区别。 根据我对苹果精美文档的理解,这种关系应该只在使用错误的情况下起作用。

我在这里缺少什么? 如何从“segmentWithDetails”获取关系对象“segment”?

I have two managed objects which have a bidirectional relationship.
It is a 'segmentWithDetails' which contains a 'segment' object.

I use

NSEntityDescription *entity = [NSEntityDescription entityForName:@"SegmentWithDetails" 
                                          inManagedObjectContext:connectionDetailsService.connectionDetailsContext];
[fetchRequest setEntity:entity];

to get my segmentWith Details. This works ok.

BUT, when I try to retrieve the contained segment, I get nil.

I've tried to get it with

Segment *segment = [segmentWithDetails valueForKeyPath:@"segment"];

and with

Segment *segment = segmentWithDetails.segment;

But this does not work (=nil). I've also tried modifying the NSFetchedResultsController.
I added the following:

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"segment"]];
[fetchRequest setIncludesSubentities:YES];

But both of these do not seem to make any difference.
From what I understand out of Apple fine documentation, the relationship should just work using faults.

What am I missing here?
How do I get to the relationship object 'segment' from 'segmentWithDetails' ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

指尖上的星空 2024-08-25 21:43:34

它应该像你描述的那样工作。您确定“段”对象/关系确实存在并且包含真实对象吗?检查数据模型是否该关系不是可选的,并检查您的代码是否实际存储了某些内容。

It should just work like you describe. Are you sure the "segment" object/relation actually exists and contains a real object? Check the data model that this relation isn't optional and check your code that you have actually stored something.

哆兒滾 2024-08-25 21:43:34

问题解决了。
这不是我如何读取数据,而是我如何将其放入 CoreData。
上面发布的代码确实是正确的。感谢 Jaanus 的回复。

Problem solved.
It was not how I read the data, but how I put it in CoreData.
The posted code above is indeed correct. Thanx for the response Jaanus.

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