Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <blockquote> <p>Can this be done with one Fetch Request or would I do 2 Requests and then merge them?<br/></p> </blockquote> </blockquote> <p>Yes it can be done with one request. <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484-SW6" rel="nofollow noreferrer">see here</a></p> <pre><code>/*UNTESTED*/ - (NSNumber*) billingSumForClient:(NSManagedObjectID*)clientId context:(NSManagedObjectContext*)context { NSNumber* total = nil; NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Billing"]; [request setResultType:NSDictionaryResultType]; NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"amount"]; NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName:@"total"]; [expressionDescription setExpression:sumExpression]; [expressionDescription setExpressionResultType:NSDateAttributeType]; [request setPredicate:[NSPredicate predicateWithFormat:@"client == %@",clientId]]; [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; NSError *error = nil; NSArray *objects = [context executeFetchRequest:request error:&amp;error]; if (&amp;error) { // Handle the error. } else { if ([objects count] &gt; 0) { total = [[objects objectAtIndex:0] valueForKey:@"total"]; } } return total; } </code></pre> <blockquote> <blockquote> <p>Can I somehow use valueForKey:@"billing.@sum.amount"?<br/></p> </blockquote> </blockquote> <p>Yes. <a href="https://stackoverflow.com/questions/7565425/core-data-get-sum-of-values-fetched-properties-vs-propagation">see here(@Daniel link)</a><br/> This might be faster if all your billings are already faulted into your current context.</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