CountForFetchRequest and executeFetchRequest return different numbers of results

I find that NSFetchRequest returns different results for the count and for the execution.

I have a Product object and a Size object. The product has many sizes.

I have two products: productA and productB. ProductA is available only in size1, and productB is available in both size1 and size2.

Given NSPredicate

ANY sizes.#size IN {"size1", "size2"}

I find that it returns 3 for countForfetchRequest, but an array of 2 elements when I execute a fetch request.

The counter value is incorrect. ProductB has both sizes and seems to be counted in twice countForfetchRequest, but explicitly returned once upon invocation executeFetchRequest.

I tried the installation setReturnsDistinctResults:YESwithout effect.

+3
2

. , , , ( ), - . , , sizeA, sizeB, sizeC .. , " " Core Data,

EDIT:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(sizes.@count == 1 || sizes.@count == 2) && (1 == SUBQUERY(sizes, $sub, $sub.#size == %@ || $sub.#size == %@).@count  || 2 == SUBQUERY(sizes, $sub, $sub.#size == %@ || $sub.#size == %@).@count)", yourSizeAvariable, yourSizeBvariable];

, .

+1

, , , SQL-. , 3, sql 3 .

, 2, . , , 3.

, select , 3 : -)

0

Source: https://habr.com/ru/post/1771555/


All Articles