Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, whenever I try something analogous to what you're doing, I get an error that you can't pass a to-many relationship to <code>setPropertiesToFetch:</code>. The <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html#//apple_ref/doc/uid/TP30001170-SW25" rel="nofollow noreferrer">NSFetchRequest documentation</a> backs this up: "The property descriptions may represent <strong>attributes, to-one relationships, or expressions.</strong>" So that's problem #1.</p> <p>Problem #2 is that it appears that you can't <em>group</em> by a to-many relationship either (this isn't made clear in the documentation, but you get the same error and it also makes sense).</p> <p>Remove "order" from the properties to fetch. Group by an attribute. Modify your main predicate to only include those attributes you group by (or just remove it). Specify "order" in your having predicate.</p> <pre><code>[fetchRequest setPropertiesToGroupBy: @[ @"???" ]]; [fetchRequest setHavingPredicate: [NSPredicate predicateWithFormat: @"self.order.@count &gt; 1"]]; </code></pre> <p>Now you'll see the request will work, but the results probably weren't what you were expecting:</p> <pre><code> - NSArray - NSDictionary { status = @"alive", orderCount = "4" } - NSDictionary { status = @"alive", orderCount = "9" } - NSDictionary { status = @"alive", orderCount = "2" } - etc... </code></pre> <p><code>NSDictionaryResultType</code> doesn't actually give you anything to identify those objects by - it just gives you the values.</p> <p>So your next step is to get back IDs for those <code>OrderedEntry</code> objects. The trick is to <a href="https://stackoverflow.com/a/4792331/1151216">include an expression</a> which will give you back the NSManagedObjectID as a key in each dictionary.</p> <p>I don't know if this will actually give you improved performance at all (over just AND-ing it in to the main predicate). In my experience, one of the best things you can do to improve fetching performance is to create singleton NSPredicates and use substitution variables to set up each fetch. Predicate parsing can be expensive.</p> <p>Dictionary result types can be a pain. Usually just constructing a good predicate is better. I tend only to use them for expressions (i.e. performing statistic-type calculations on the graph which return a value). If you look at all the restrictions around properties to fetch and group by and the predicate restrictions, this seems to be what Apple intend it for. I'm not even sure it's possible to do what you want to do by grouping and using a having predicate - for example, what are you going to group by? If by status (which you need to group by to include in your predicate), won't that collapse all the OrderEntries and you won't get the separate objectIDs and orderCounts? It's best to stick to the main predicate for this, not grouping and having predicates.</p>
 

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