如何创建一个 Core Data 谓词来测试一个关系是否包含所有给定的对象? [英] How to create a Core Data predicate to test that a relation contains all given objects?

查看:19
本文介绍了如何创建一个 Core Data 谓词来测试一个关系是否包含所有给定的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个核心数据对象 A,它与 B 有一对多的关系.称这种关系为项目".因此,a.items 返回与 A 关联的所有 B-s.

I have a Core Data object A that has a to-many relation to B. Call the relation "items". So, a.items returns all B-s associated with A.

现在,我有一个 B 对象的手动组合 NSSetitemSet".

Now, I have a manually composed NSSet "itemSet" of B objects.

我想做以下事情:

return all A objects whose "items" relation exactly matches itemSet

我如何为此构造一个谓词?我试过了:

How do I construct a predicate for that? I’ve tried this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(ALL items in %@)", itemSet];

但这只是给了我不支持的谓词(空).

这个:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(items in %@)", itemSet];

告诉我谓词的未实现的 SQL 生成.有趣,但没有帮助.

tells me unimplemented SQL generation for predicate. Interesting, but not helpful.

那么用集合过滤关系的正确方法是什么?

So what’s the right way to filter the relation with a set?

推荐答案

以下谓词可能有效:

[NSPredicate predicateWithFormat:@"(items.@count == %d) AND (SUBQUERY(items, $x, $x IN %@).@count == %d)",
                      itemSet.count, itemSet, itemSet.count];

谓词首先检查项目的数量是否等于给定itemSet的大小,然后检查作为itemSet成员的项目的数量是也等于itemSet的大小.如果两者都为真,则 items 必须等于 itemSet.

The predicate checks first that the number of items is equal to the size of the given itemSet, and then checks that the number of items which are member of itemSet is also equal to the size of itemSet. If both are true, then items must be equal to itemSet.

这篇关于如何创建一个 Core Data 谓词来测试一个关系是否包含所有给定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆