具有多对多关系的核心数据 - 在 SUBQUERY 中创建带有 ALL 的 NSPredicate
我的对象图看起来像这个
快照 -->>窗格 --> 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个好的经验法则是,如果您已经拥有托管对象,则无需获取,而是将关系从您拥有的托管对象转移到您想要的托管对象。
因此,您的关系图实际上可能如下所示:
或者可能:
由于您有一组
ManagedImage
对象,您所要做的就是遍历pane.snapShot
的键路径或panes.snapShots
查找与每个ManagedImage
对象关联的SnapShot
对象。然后,您只需提取唯一的 SnapShot 对象。在第一种情况下,问题很简单,因为
在第二种情况下,您需要首先获取所有唯一的 SnapShot 对象:
... 对于每个
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:
or maybe:
Since you have a set of
ManagedImage
objects all you have to do is walk the keypath ofpane.snapShot
orpanes.snapShots
to find theSnapShot
objects associated with eachManagedImage
object. Then you just extract the uniqueSnapShot
objects.In the first case, the matter is trivial because of the one-to-one relationship path of
In the second case, you will need to first get all the unique SnapShot objects:
... for each
ManagedImage
instances and then merge all the sets withsetByAddingObjectsFromSet:
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.