Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you construct the predicate for NSFetchRequest setHavingPredicate:?
    primarykey
    data
    text
    <p>For the life of me I can not seem to get this to work.</p> <p>Assume our entity is an managed object with a status field and an order field.</p> <p>How would I go about getting all orderedEntries having more than one order that are the same?</p> <p><strong>Please no answers telling me to just do a subquery with @count in the main predicate, since I know of that solution, the point of this post is to understand how to use the having predicate in core data, which would probably be faster than a subquery anyways. (unless you explain why I can not use a having clause)</strong></p> <p>The following code would return an array of dictionaries with the number of orders per order number. What I want is to be able to add a having clause to restrict my request to only return the dictionaries representing objects of those orders that have a count greater than 1.</p> <p>Here is the code so far and my attempts at a having predicate:</p> <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"OrderedEntry" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; [fetchRequest setResultType:NSDictionaryResultType]; NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(status == %@)",[NSNumber numberWithInt:EntryStatusAlive]]; [fetchRequest setPredicate:predicate]; NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"order"]; // Does not really matter NSExpression *maxExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName: @"orderCount"]; [expressionDescription setExpression: maxExpression]; [expressionDescription setExpressionResultType: NSInteger32AttributeType]; [fetchRequest setPropertiesToFetch: [NSArray arrayWithObjects:expressionDescription,@"order",nil]]; [fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:@"order",nil]]; //[fetchRequest setHavingPredicate:[NSPredicate predicateWithFormat:@"@count &gt; 1"]]; //[fetchRequest setHavingPredicate:[NSComparisonPredicate predicateWithLeftExpression:maxExpression rightExpression:[NSExpression expressionForConstantValue:[NSNumber numberWithInteger:1]] modifier:NSDirectPredicateModifier type:NSGreaterThanPredicateOperatorType options:NSCaseInsensitivePredicateOption]]; NSError *error; NSArray * array = [context executeFetchRequest:fetchRequest error:&amp;error]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload