本文介绍了通过核心数据的不同计数,NSExpression到NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 目前使用核心数据。我有一个表,其中我试图检索这些行的信息:Currently using Core Data. I have one table in which I am trying to retrieve information along these lines:SELECT item, COUNT(*) FROM myTable GROUP BY item;以产生此类型的结果:+---------+------+-------+| item | COUNT(*) |+---------+------+-------+| group 1 | 2 || group 2 | 5 || group 3 | 8 |+---------+------+-------+我有一个明智的想法,使用 NSExpression ,希望有核心数据为我做所有的工作。我开始旋转我的轮子。 count:表达式函数在崩溃。例外不是很清楚。使用其他表达式函数(如 sum:)不会导致应用程序崩溃。I had the bright idea to use an NSExpression, with hopes of having Core Data do all the work for me. I am beginning to spin my wheels. The count: expression function is crashing on me. The exception is not very clear. The use of other expression functions, such as sum: doesn't crash the app.将结果存储在 NSFetchedResultsController 中会很不错。我探讨了其他选择,没有一个不太吸引人。在这种情况下,编写SQL查询并使用,而不是使用Core Data作为SQL包装更有意义吗?It would be nice to store the results in a NSFetchedResultsController. I've explored other options, none of which aren't too appealing. Would it make more sense to write a SQL query and get it over with, rather than to use Core Data as a SQL wrapper in this case?您的参考源代码如下。NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"item"];NSExpression *totalExpression = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:keyPathExpression]];NSExpressionDescription * totalExpressionDescription = [[NSExpressionDescription alloc] init];[totalExpressionDescription setExpression:totalExpression];[totalExpressionDescription setExpressionResultType:NSInteger64AttributeType];[totalExpressionDescription setName:@"totalInstances"];NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];NSEntityDescription *entity = [NSEntityDescription entityForName:@"myEntity" inManagedObjectContext:self.managedObjectContext];NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:strFieldName, totalExpressionDescription, nil];[fetchRequest setEntity:entity];[fetchRequest setReturnsDistinctResults:YES];[fetchRequest setResultType:NSDictionaryResultType];[fetchRequest setPropertiesToFetch:propertiesToFetch];[fetchRequest setFetchBatchSize:20];NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:strFieldName ascending:YES];NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];[fetchRequest setSortDescriptors:sortDescriptors];NSFetchedResultsController* aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];...NSError *error = nil;if (![self.fetchedResultsController performFetch:&error]){ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort();}推荐答案 NSExpression你最初提出,在我设计的例子中,我有一个名为Person的实体,具有名为emailAddress的属性,我希望获得计数。This is possible with NSExpression as you originally proposed, in my contrived example I have an entity called "Person" with a property called "emailAddress" that I wish to get the count for.NSPropertyDescription *propDesc = [[[[model entitiesByName] objectForKey:@"Person"] propertiesByName] objectForKey:@"emailAddress"];NSExpression *emailExpr = [NSExpression expressionForKeyPath:@"emailAddress"];NSExpression *countExpr = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:emailExpr]];NSExpressionDescription *exprDesc = [[NSExpressionDescription alloc] init];[exprDesc setExpression:countExpr];[exprDesc setExpressionResultType:NSInteger64AttributeType];[exprDesc setName:@"count"];NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Person"];[fr setPropertiesToGroupBy:[NSArray arrayWithObject:propDesc]];[fr setPropertiesToFetch:[NSArray arrayWithObjects:propDesc, exprDesc, nil]];[fr setResultType:NSDictionaryResultType];NSArray *results = [moc executeFetchRequest:fr error:&error];我写了一段代码来预填充数据库1000条记录:I wrote a snippet of code to pre-populate the database with 1000 records:NSArray *emailAddresses = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", nil]; for (int i = 0; i < 1000; i++) { NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:moc]; [person setValue:[NSNumber numberWithInt:i] forKey:@"aNumber"]; [person setValue:[[NSUUID UUID] UUIDString] forKey:@"aUUID"]; [person setValue:[emailAddresses objectAtIndex:(i % [emailAddresses count])] forKey:@"emailAddress"]; }The above code will insert each email address 200 times, here are the results:2012-05-31 15:17:42.160 Scratch[16084:10d03] CoreData: sql: SELECT t0.ZEMAILADDRESS, COUNT( t0.ZEMAILADDRESS) FROM ZPERSON t0 GROUP BY t0.ZEMAILADDRESS2012-05-31 15:17:42.162 Scratch[16084:10d03] CoreData: annotation: sql connection fetch time: 0.0024s2012-05-31 15:17:42.163 Scratch[16084:10d03] CoreData: annotation: total fetch execution time: 0.0029s for 5 rows.(gdb) po results(NSArray *) $2 = 0x0f811280 <_PFArray 0xf811280>({ count = 200; emailAddress = "[email protected]";},{ count = 200; emailAddress = "[email protected]";},{ count = 200; emailAddress = "[email protected]";},{ count = 200; emailAddress = "[email protected]";},{ count = 200; emailAddress = "[email protected]";}) 这篇关于通过核心数据的不同计数,NSExpression到NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 13:37