我真的很努力地在新的IOS应用程序中使用coredata。
真正的挑战是,由于我的背景是SQL,我一直在思考SQLite而不是coredata。我已经解决了大多数障碍,但是这个障碍让我感到困惑 - 我真的想创建一个返回group_concat的SQLite视图。
我是否需要通过在objective-c中对结果进行编码来重新创建它,或者是否有办法使用coredata对象创建视图或发出直接SQL?
编辑:添加了数据模型和所需结果
表-A;列 - 键,名称(注释 - 键不唯一)
样本数据:
K1,N1
K1,N2
K1,N3
K2,N1
K2,N2
期望的结果:
K1,N1; N2; N3
K2,N1; N2
在SQLite中,这将是来自Table_A GROUP BY键的SELECT键,GROUP_CONCAT(名称)
答案 0 :(得分:1)
如果您将获取请求设置为以字典形式返回结果,则可以对分组进行大量控制。
http://mattconnolly.wordpress.com/2012/06/21/ios-core-data-group-by-and-count-results/和Core Data Fetching Properties with Group By Count都包含很好的例子。第二个链接显示了如何沿着关系的键路径进行分组。
编辑:
在看到修改后的问题后,我对此进行了修改。源代码在Github上:https://github.com/halmueller/CoreDataGroupFetch
我无法获得可在单个查询中使用的解决方案。这是我能想到的最好的。它获取“key”字段的所有唯一值,然后迭代这些键并获取与“key”匹配的所有对象。在第二次获取中,您可能希望获取NSManagedObjects而不是字典,具体取决于您要执行的操作。
NSFetchRequest* uniqueKeysFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"KeyedName"];
uniqueKeysFetchRequest.propertiesToFetch = @[@"key"];
uniqueKeysFetchRequest.resultType = NSDictionaryResultType;
uniqueKeysFetchRequest.returnsDistinctResults = YES;
NSError* error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:uniqueKeysFetchRequest
error:&error];
NSLog(@"uniqueKeysFetchRequest: %@", results);
NSLog(@"distinct values for \"key\": %@", [results valueForKeyPath:@"@distinctUnionOfObjects.key"]);
for (NSString *thisKey in [results valueForKey:@"key"]) {
NSFetchRequest *oneKeyFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"KeyedName"];
NSString *predicateString = [NSString stringWithFormat:@"key LIKE '%@'", thisKey];
oneKeyFetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];
oneKeyFetchRequest.resultType = NSDictionaryResultType;
oneKeyFetchRequest.propertiesToFetch = @[@"name"];
NSLog(@"%@: %@", thisKey, [self.managedObjectContext executeFetchRequest:oneKeyFetchRequest error:&error]);
}
这会产生
results from uniqueKeysFetchRequest: (
{
key = K1;
},
{
key = K2;
}
)
distinct values for "key": (
K1,
K2
)
K1: (
{
name = N2;
},
{
name = N1;
},
{
name = N3;
}
)
K2: (
{
name = N2;
},
{
name = N1;
}
)
我也试过
NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"KeyedName"];
fetchRequest.propertiesToFetch = @[@"key", @"name"];
fetchRequest.propertiesToGroupBy = @[@"key", @"name"];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.returnsDistinctResults = YES;
NSError* error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSLog(@"withKeypathStrings: %@", results);
NSLog(@"distinct values for \"key\": %@", [results valueForKeyPath:@"@distinctUnionOfObjects.key"]);
可能更接近你想要的东西:
withKeypathStrings: (
{
key = K1;
name = N1;
},
{
key = K1;
name = N2;
},
{
key = K1;
name = N3;
},
{
key = K2;
name = N1;
},
{
key = K2;
name = N2;
}
)
distinct values for "key": (
K2,
K1
)
答案 1 :(得分:0)
您可以使用NSExpression(forFunction:“ count:”)或编号为https://developer.apple.com/documentation/foundation/nsexpression/1413747-init的其他函数来实现带有Core Data的group_concat 但是由于Core Data不支持自定义NSExpression函数,因此无法像“ name1,name2,name3 ...”这样的字符串连接。