- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
目前正在使用核心数据。我有一张表,我试图在其中按照以下方式检索信息:
SELECT item, COUNT(*) FROM myTable GROUP BY item;
为了产生这种类型的结果:
+---------+------+-------+
| item | COUNT(*) |
+---------+------+-------+
| group 1 | 2 |
| group 2 | 5 |
| group 3 | 8 |
+---------+------+-------+
我有了使用 NSExpression 的好主意,希望让 Core Data 为我完成所有工作。我开始转动我的轮子。 count: 表达式函数在我身上崩溃了。异常(exception)不是很清楚。使用其他表达式函数(例如 sum:)不会使应用程序崩溃。
最好将结果存储在 NSFetchedResultsController 中。我探索了其他选项,没有一个不太吸引人。在这种情况下,编写 SQL 查询并完成它比使用 Core Data 作为 SQL 包装器更有意义吗?
供您引用的源代码如下。
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”的属性,我希望获得它的计数。
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 条记录预填充数据库:
NSArray *emailAddresses = [NSArray arrayWithObjects:@"test@test.com", @"1@1.com", @"1@2.com", @"roam@test.com", @"foo@foo.com", 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"];
}
上面的代码将每个电子邮件地址插入 200 次,结果如下:
2012-05-31 15:17:42.160 Scratch[16084:10d03] CoreData: sql: SELECT t0.ZEMAILADDRESS, COUNT( t0.ZEMAILADDRESS) FROM ZPERSON t0 GROUP BY t0.ZEMAILADDRESS
2012-05-31 15:17:42.162 Scratch[16084:10d03] CoreData: annotation: sql connection fetch time: 0.0024s
2012-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 = "1@1.com";
},
{
count = 200;
emailAddress = "1@2.com";
},
{
count = 200;
emailAddress = "foo@foo.com";
},
{
count = 200;
emailAddress = "roam@test.com";
},
{
count = 200;
emailAddress = "test@test.com";
}
)
关于iphone - 通过核心数据的不同计数,NSExpression 到 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157436/
我正在尝试使用具有混合语言数据的 FRC,并希望有一个部分索引。 从文档看来您应该能够覆盖 FRC - (NSString *)sectionIndexTitleForSectionName:(NSS
使用等效的 NSFetchRequest 重新创建 NSFetchedResultsController 时,为什么 [NSFetchedResultsController PerformFetch:
我正在尝试起草一个可重复使用的类,该类基本上可重复用于具有几乎相同实现的多个实体。基本上我将有几个 UIViewController,每个都包含 UIView,其中包含 UITableView 以及由
我有一个 iPhone 和一个 Android 应用程序。在 iPhone 上,我使用 NSFetchedResultsController 来管理我的 UITableView 的数据。目前我有很多数
NSFetchedResultsController 监视对正在跟踪的整个托管对象的更改。每当在当前上下文中修改任何属性时,例如 – controller:didChangeObject:atInde
自 1 个月以来,我一直在使用 NSFetchedResultsController 构建应用程序,并在 3.1.2 SDK 上测试该应用程序。问题是我一直在我的应用程序中到处使用 NSFetched
我有一个人物和宠物的数据库,具有一对多关系 人宠物 姓名 姓名 宠物>主人 我正在使用由核心数据支持的 UITableView 和 nsfetchedresultscontroller 来显示宠物列表
我有一个 NSFetchedResultsController,它在 TableView 中显示项目列表,包括关联实体的计数。当为此关联添加对象时(使用 addXXXObject),不会调用回调来通知
花了几个小时后,我发现无法在以下文章中由 SQLite 支持的 NSFetchedResultsController 中进行自定义排序。 NSFetchedResultsController cust
我设置了一个 NSFetchedResultsController 来填充 UITableView,基于我的“主”NSManagedObjectContext。 在计时器中,我不断地将对象添加到单独的
在我的应用程序中,我需要循环遍历核心数据中的所有实体,并且我正在使用 NSFetchedresultcontroller。 我现在正在这样做: NSArray *tempArray = [[NSArr
我正在开发我的第一个核心数据支持的应用程序,但无法弄清楚如何正确设置 NSFetchedResultsController。我有两个实体: /-----------\ /--------
我正在使用 NSFetchedResultsController 在 TableView 中显示项目: - (NSInteger)numberOfSectionsInTableView:(UITabl
我将 NSFetchedResultsController 放入我的代码中,这样我就可以对 TableView 数据进行很好的自动分段。 所以我正在运行测试以确保一切正常。我的持久存储中有一个 Boo
我如何使用带有翻译后的排序键和sectionKeyPath的NSFetchedResultsController? 问题:我在数据库中的属性“type”中有 ID,例如 typeA、typeB、typ
大家好 我的 CoreData 驱动应用程序有多个来自同一存储的数据 View ,并且全部使用 NSFetchedResultsController。目前,它们的 NSFetchedResultsCo
我想使用使用 bool 值设置的不同谓词从 NSFetchedResultsController 重新获取数据。如何刷新 NSFetchedResultsController 以获取一组新数据? -
核心数据在 NSManagedObjectContext 类中提供了“executeFetchRequest”方法,我们可以使用该方法从表中获取数据并以任何需要的方式使用它。 现在还有另一种方法,即使
我正在编写一个应用程序,一个屏幕上有两个表格。左表是文件夹列表,右表显示文件列表。当点击左侧的一行时,右侧表格将显示属于该文件夹的文件。 我正在使用 Core Data 进行存储。当文件夹的选择发生变
我有一个由 NSFetchedResultsController 管理的 TableView 。但是,我在查找或创建操作时遇到问题。当用户点击表格 View 的底部时,我正在向服务器查询另一批内容。如
我是一名优秀的程序员,十分优秀!