How to count in coredata (aggregation)?

I study basic data and especially work on aggregation.

The current thing I want to do: count the number of records from a table, which is in many ways inversely dependent on some criteria.

I am currently doing this:

NSExpression *ex = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"ddname"]]]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"]; NSExpressionDescription *ed = [[NSExpressionDescription alloc] init]; [ed setName:@"countDDEvents"]; [ed setExpression:ex]; [ed setExpressionResultType:NSInteger16AttributeType]; NSArray *properties = [NSArray arrayWithObject:ed]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setPredicate:pred]; [request setPropertiesToFetch:properties]; [request setResultType:NSDictionaryResultType]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]]; [request setEntity:entity]; NSArray *results = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:nil]; NSDictionary *dict = [results objectAtIndex:0]; NSLog(@"Average birthdate for female heroes: %@", [dict objectForKey:@"countDDEvents"]); 

Its from jeff lemarche.

EDIT : and I found my solution as

  NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"]; [request setPredicate:pred]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]]; [request setEntity:entity]; NSError *error = nil; NSUInteger count = [[self.currentAccount managedObjectContext] countForFetchRequest:request error:&error]; 

It works beautifully. But I want to do more queries of this type at a time. Therefore, I think this may not be the preferred way to get an invoice.

EDIT :

So, I think the approach would be appropriate.

So can anyone tell me a more efficient preferred way to do this.

Thanks.

+6
iphone aggregation count core-data
source share
2 answers

Jeff LaMarche just uses this as a simple example. In practice, this need is so widespread that Key-Value Coding has a built-in macro for processing and other general collection operations.

See: Key Value Programming Guide: Dial and Array Operators

In this case, you would use the @count operator in your predicate.

Of course, manually adjusting your own expression gives you excellent control over your predicates, but operators handle 80% of this task.

+4
source share

I had to count about 10,000 objects, and this slowed down my sensitivity to the interface by doing this with countForFetchRequest.

Here is a way to do this using NSExpression:

 - (NSUInteger) unfilteredFCsCount { // Just the fetchRequest NSFetchRequest *fetchRequest = [self unfilteredFCsFetchRequest]; [fetchRequest setResultType: NSDictionaryResultType]; // You can use any attribute of the entity. its important, because you are not counting // the properties, but actually the entities NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"sortIndex_"]; // Does not really matter NSExpression *maxExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName: @"fcCount"]; [expressionDescription setExpression: maxExpression]; [expressionDescription setExpressionResultType: NSInteger32AttributeType]; [fetchRequest setPropertiesToFetch: [NSArray arrayWithObject:expressionDescription]]; NSUInteger fcCount = 0; NSError *error = nil; NSArray *results = nil; results = [self.managedObjectContext executeFetchRequest: fetchRequest error: &error]; KSLog(KSLogLevelDebug, @"unfilteredFCsCount results: %@", results); if([results count] > 0) { NSNumber *count = [[results objectAtIndex: 0] objectForKey: @"fcCount"]; fcCount = [count intValue]; } return fcCount; } 
+5
source share

All Articles