countForFetchRequest 和executeFetchRequest 返回不同数量的结果
我发现 NSFetchRequest 返回不同的计数和执行结果。
我有一个产品实体和一个尺寸实体。一个产品有多种尺寸。
我有两个产品,产品A和产品B。产品 A 仅提供尺寸 1,产品 B 提供尺寸 1 和尺寸 2。
给定 NSPredicate
'ANY size.#size IN {"size1", "size2"}
'
我发现它为 countForfetchRequest
返回 3,但返回的是 2 的数组当我执行获取请求时的项目。
计数是不正确的值。 ProductB 具有两种大小,并且似乎在 countForfetchRequest
中计数了两次,但显然仅由 executeFetchRequest
调用返回一次。
我尝试设置 setReturnsDistinctResults:YES
但没有效果。
I'm finding that a NSFetchRequest is returning different results for a count and for an execute.
I have a Product entity and a Size entity. A Product has many Sizes.
I have two products, productA and productB. ProductA is only available in size1 and productB is available in both size1 and size2.
Given the NSPredicate
'ANY sizes.#size IN {"size1", "size2"}
'
I'm finding that it returns 3 for the countForfetchRequest
but an array of 2 items when I execute the fetch request.
The count is the value that is incorrect. ProductB has both sizes and seems to be being counted twice in the countForfetchRequest
but obviously only returned once by the executeFetchRequest
call.
I've tried setting setReturnsDistinctResults:YES
with no effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我稍微修改了答案中的谓词。请注意,有关您可能遇到的解析问题的相同注意事项(在我之前的评论之一中给出)仍然适用。我假设您有多个尺寸,例如 sizeA、sizeB、sizeC 等。您需要一个子查询来正确处理核心数据中的一对多关系,如以下谓词所示
编辑:
让我知道这是否适用于你。
I have slightly modified the predicate in the answer. Note that the same considerations regarding the parsing problems you may encounter (given in one of my previous comment) still apply. I am assuming that you have multiple sizes, say sizeA, sizeB, sizeC etc. You need a subquery to deal correctly with your to-many relationship in Core Data, as shown in the following predicate
EDIT:
Let me know if this works correctly for you.
我认为当你进行计数时,它是在计算从 sql 查询返回的行数。当您查询大小表时,您会返回 3,因为等效的 sql 将返回 3 行。
当您进行提取时,您会得到 2,因为您只会返回两个产品对象。但是,如果您查看这些产品内部的尺寸对象数量,您会发现 3 个。
哦,选择不同的值将不起作用,因为您有 3 个唯一的值集:-)
I think when you do a count, it is counting the number of rows returned from the sql query. As you are querying the size table, you get back 3 because the equivalent sql would return 3 rows.
When you do a fetch, you get 2 because you will only get back two product objects. However if you looked inside those products to the number of size objects, you will find 3.
Oh, and the select distinct will not work because you have 3 unique sets of values :-)