具有多对多关系的核心数据 - 在 SUBQUERY 中创建带有 ALL 的 NSPredicate

发布于 2024-11-25 18:17:55 字数 864 浏览 1 评论 0原文

我的对象图看起来像这个

快照 -->>窗格 --> ManagedImage

我试图找到一个快照,该快照具有包含在集合中的确切 ManagedImages。

我现在得到的代码返回一组快照,其中包含该集中的一个或多个 ManagedImage。然后,我搜索数组以找到正确的快照,但我猜测使用 NSPredicate 在子查询中进行过滤会更快,

如何获得包含集合中所有 ManagedImages 的唯一快照?

这是我的代码

mySet = ... // A unique set of (usually 3) managedImages that I'm trying to find a snapShot for

NSFetchRequest *request = ...
request.entity = [NSEntityDescription entityForName:@"SnapShot" inManagedObjectContext:[self managedObjectContext]];

// Want this to work but sends an exception
//request.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(self.panes, $pane, ALL $pane.managedImage IN %@).@count != 0", mySet];

// Using this
request.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(self.panes, $pane, $pane.managedImage IN %@).@count != 0", mySet];

My object graph looks like this

SnapShot -->> Pane --> ManagedImage

I'm trying to find a SnapShot that has the exact ManagedImages contained with in a set.

The code I've got now returns an Array of SnapShots that have one or more of the ManagedImages that are in the set. I then search through the Array to find the correct SnapShot but I'm guessing it would be much faster to filter in the Subquery

With an NSPredicate how can I get the unique SnapShot that has ALL of the ManagedImages that are in the set?

Here's my code

mySet = ... // A unique set of (usually 3) managedImages that I'm trying to find a snapShot for

NSFetchRequest *request = ...
request.entity = [NSEntityDescription entityForName:@"SnapShot" inManagedObjectContext:[self managedObjectContext]];

// Want this to work but sends an exception
//request.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(self.panes, $pane, ALL $pane.managedImage IN %@).@count != 0", mySet];

// Using this
request.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(self.panes, $pane, $pane.managedImage IN %@).@count != 0", mySet];

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

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

发布评论

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

评论(1

伪装你 2024-12-02 18:17:55

一个好的经验法则是,如果您已经拥有托管对象,则无需获取,而是将关系从您拥有的托管对象转移到您想要的托管对象。

因此,您的关系图实际上可能如下所示:

SnapShot <-->> Pane <--> ManagedImage

或者可能:

SnapShot <<-->> Pane <<--> ManagedImage

由于您有一组 ManagedImage 对象,您所要做的就是遍历 pane.snapShot 的键路径或panes.snapShots 查找与每个 ManagedImage 对象关联的 SnapShot 对象。然后,您只需提取唯一的 SnapShot 对象。

在第一种情况下,问题很简单,因为

ManagedImage-->Pane-->SnapShot

在第二种情况下,您需要首先获取所有唯一的 SnapShot 对象:

NSSet *shots=[aMangedImageObj valueForKeyPath:@"distinctUnionOfSets.panes.snapShots"];

... 对于每个 ManagedImage实例,然后使用 setByAddingObjectsFromSet: 或类似方法合并所有集合以生成一组唯一对象。

提取应该用于查找图中您需要的第一个对象,但是一旦您拥有了对象,您就不会提取而是遍历关系。否则,一开始建立关系就没有多大意义。

A good rule of thumb is that if you already have managed objects in hand, you don't fetch but instead walk the relationships from the managed objects you have to the managed objects you want.

So, your relationship graph probably actually looks like this:

SnapShot <-->> Pane <--> ManagedImage

or maybe:

SnapShot <<-->> Pane <<--> ManagedImage

Since you have a set of ManagedImage objects all you have to do is walk the keypath of pane.snapShot or panes.snapShots to find the SnapShot objects associated with each ManagedImage object. Then you just extract the unique SnapShot objects.

In the first case, the matter is trivial because of the one-to-one relationship path of

ManagedImage-->Pane-->SnapShot

In the second case, you will need to first get all the unique SnapShot objects:

NSSet *shots=[aMangedImageObj valueForKeyPath:@"distinctUnionOfSets.panes.snapShots"];

... for each ManagedImage instances and then merge all the sets with setByAddingObjectsFromSet: or a similar method to produce a single set of unique objects.

Fetches should be used to find the first objects in graph that you need but once you have the objects, you don't fetch but walk the relationships. Otherwise, there is not much point to having relationships in the first place.

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