【发布时间】:2015-08-23 17:17:42
【问题描述】:
我想在iOS CoreData 中实现一个SQL 查询,这是我的查询。
Select * from table group by my_field1 order by my_field2
我想显示属性my_field1 的GroupBy,但我收到默认值不能为零的错误。
这是我的代码:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"myentity"];
[fetchRequest setResultType:NSDictionaryResultType];
fetchRequest.propertiesToGroupBy = [NSArray arrayWithObject:@"my_field1"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"my_field2" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSMutableArray *data = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
如何在 Coredata 中按 Group by 属性过滤结果。
我想要的结果是:
我的表格数据:
my_field1 | my_field2 | my_field3
1 ABC DEF
1 GHI JKL
1 MNO PQR
2 DEF DEF
我的预期结果:
my_field1 | my_field2 | my_field3
1 MNO PQR
2 DEF DEF
我期待为 my_field1 找到最后一行,因为我也在查询中包含 order_by。
【问题讨论】:
-
你想获得 values "1", "MNO" and "PQR" (and "2", "DEF" and "DEF"), 还是做你想要具有这些值的 NSManagedObjects 吗?
标签: ios objective-c core-data