如何获取通过关系检索的 NSManagedObject 的 ObjectID?

发布于 2024-11-02 17:18:40 字数 545 浏览 8 评论 0原文

我有一个关键字查询的结果集。返回的每个关键字对象与另一个实体都具有多对多关系。 (称之为 B )

我想知道有没有一种方法可以使用关键字 resultset 并仅查找 B 实体的托管 objectID,而不必将它们拉入内存?目前,我一直在这样做:

NSMutableArray *objIdList = [[NSMutableArray alloc] init];
NSArray *keywordResultSet = <results from initial keyword query>;
for ( Keyword *keyword in keywordResultSet )
{
    B *mo = [keyword valueForKey: <relationship>];
    [objIdlist addObject: [mo objectID]];
}
// additional sorting of result set

问题是当我这样做时速度非常慢。有没有办法只获取ObjectID?

I have a resultset from a keyword query. Each keyword object returned has a many-to-many relationship to another entity. ( call it B )

What I'm wondering is there a way to use the keyword resultset and find only the managed objectIDs to the B entities without having to pull them into memory? Currently, I've been doing this:

NSMutableArray *objIdList = [[NSMutableArray alloc] init];
NSArray *keywordResultSet = <results from initial keyword query>;
for ( Keyword *keyword in keywordResultSet )
{
    B *mo = [keyword valueForKey: <relationship>];
    [objIdlist addObject: [mo objectID]];
}
// additional sorting of result set

The problem is the it is SUPER slow when I do this. Is there a way to only get the ObjectIDs?

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

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

发布评论

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

评论(1

沧笙踏歌 2024-11-09 17:18:40

首先要问的是您是否需要 objectID。许多 Core Data 新手认为 objectID 就像关系数据库中的键。他们不是。仅当需要引用另一个持久存储中的对象时才需要 objectID。否则,它们对你没有多大好处。我认为,考虑到您对核心数据相对缺乏经验,您实际上需要首先获取 objectID 的可能性极小。

对于除 objectID 之外的任何其他属性,您可以按属性进行提取,并将结果类型设置为字典,这会生成轻量级提取,该提取仅返回包含所查找的一个属性的值的字典数组。您不能使用 objectID 执行此操作,因为严格来说,objectID 并不是托管对象的属性,而是在特定持久存储中对它们的引用。

The first thing to ask is if you need objectIDs at all. A lot of Core Data novices believe that objectIDs are like keys in a relational database. They aren't. You only need objectIDs when you have a need to refer to objects in another persistent store. Otherwise, they don't do you much good. I think it highly unlikely, given your relative inexperience with Core Data, that you actually need to obtain objectIDs in the first place.

For any other attribute other than objectIDs, you could do a fetch by attribute and set the result type to dictionary which produces a lightweight fetch that returns only an array of dictionaries containing the value of the one attribute sought. You can't do that with objectIDs because the objectIDs are not strictly speaking attributes of the managed objects but rather references to them in a particular persistent store.

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