可以将group_concat与coredata一起使用吗? [英] Is it possible to use group_concat with coredata?

查看:81
本文介绍了可以将group_concat与coredata一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很努力在新的IOS应用程序中使用coredata。



一个真正的挑战是,由于我的背景是SQL,所以我一直在考虑SQLite而不是coredata。我已经解决了大多数障碍,但是这一点让我很头疼–我真的很想创建一个返回group_concat的SQLite视图。



我是否需要通过对Objective-C中的结果进行编码来重新创建此视图,还是有一种使用coredata对象创建视图或直接发布SQL的方法? p>

编辑:添加了数据模型和所需结果



Table_A;列-键,名称(注意-键不是唯一的)



样本数据:

K1,N1

K1,N2

K1,N3

K2,N1

K2,N2



所需结果:

K1,N1; N2; N3

K2,N1; N2



在SQLite中,这将是SELECT键, Table_A GROUP BY键中的GROUP_CONCAT(name)

解决方案

如果您将获取请求设置为以字典形式返回结果,则有很多控制分组。



http://mattconnolly.wordpress.com/2012/06/21/ios-core-data-group-by-and-count-results/ 具有按组分组的核心数据获取属性都包含很好的例子。第二个链接显示了如何沿关系的关键路径进行分组。



编辑:



在看到修改后的问题后,我对此进行了一些修改。源代码在Github上: https://github.com/halmueller/CoreDataGroupFetch



我无法获得可以在单个查询中使用的解决方案。这是我能想到的最好的。它获取键字段的所有唯一值,然后遍历这些键并获取与该键匹配的所有对象。在第二个访存中,您可能想取NSManagedObjects而不是字典,这取决于您要执行的操作。

  NSFetchRequest * uniqueKeysFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@ KeyedName]; 

uniqueKeysFetchRequest.propertiesToFetch = @ [@键];
uniqueKeysFetchRequest.resultType = NSDictionaryResultType;
uniqueKeysFetchRequest.returnsDistinctResults = YES;

NSError *错误=无;
NSArray * results = [self.managedObjectContext executeFetchRequest:uniqueKeysFetchRequest
error:& error];
NSLog(@ uniqueKeysFetchRequest:%@,结果);

NSLog(@键的不同值:%@,[结果valueForKeyPath:@ @ distinctUnionOfObjects.key]);

for(NSString * thisKey in [resultsvalueForKey:@ key])){
NSFetchRequest * oneKeyFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@ KeyedName];
NSString * predicateString = [NSString stringWithFormat:@键类似'%@',thisKey];
oneKeyFetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];
oneKeyFetchRequest.resultType = NSDictionaryResultType;
oneKeyFetchRequest.propertiesToFetch = @ [@名称];
NSLog(@%@:%@,thisKey,[self.managedObjectContext executeFetchRequest:oneKeyFetchRequest error:& error]);
}

这将产生

 来自uniqueKeysFetchRequest的结果:(
{
键= K1;
},
{
键= K2;
}

键的不同值:(
K1,
K2

K1:(
{
名称= N2;
},
{
名称= N1;
},
{
名称= N3;
}

K2:(
{
名称= N2;
},
{
名称= N1;
}

我也尝试过

  NSFetchRequest * fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@ KeyedName]; 

fetchRequest.propertiesToFetch = @ [@键,@名称];
fetchRequest.propertiesToGroupBy = @ [@键,@名称];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.returnsDistinctResults = YES;

NSError *错误=无;
NSArray * results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:& error];
NSLog(@ withKeypathStrings:%@,结果);

NSLog(@键的不同值:%@,[结果valueForKeyPath:@ @ distinctUnionOfObjects.key]);

可能更接近您想要的内容:

  withKeypathStrings:(
{
键= K1;
名称= N1;
},
{
键= K1;
名称= N2;
},
{
键= K1;
名称= N3;
},
{
键= K2;
名称= N1;
},
{
键= K2;
名称= N2;
}

键的不同值:(
K2,
K1


I am really trying hard to use coredata with a new IOS application.

A real challenge is that since my background is SQL, I keep thinking in terms of SQLite rather than coredata. I've solved most hurdles, but this one stumps me -- I really want to create a SQLite view that returns a group_concat.

Do I need to recreate this by coding on the results in objective-c, or is there a way to create a view or issue direct SQL using coredata objects?

EDIT: Added data model and desired results

Table_A; columns -- key, name (note -- key is not unique)

Sample data:
K1, N1
K1, N2
K1, N3
K2, N1
K2, N2

Desired results:
K1, N1;N2;N3
K2, N1;N2

In SQLite this would be SELECT key, GROUP_CONCAT(name) from Table_A GROUP BY key

解决方案

If you set your fetch request to return results as dictionaries, you have a lot of control over grouping.

http://mattconnolly.wordpress.com/2012/06/21/ios-core-data-group-by-and-count-results/ and Core Data Fetching Properties with Group By Count both contain good examples. The second link shows how to walk down a relationship's keypath for grouping.

EDIT:

After seeing revised question, I tinkered with this a bit. Source code is on Github: https://github.com/halmueller/CoreDataGroupFetch

I couldn't get a solution that would work in one single query. This is the best I could come up with. It fetches all of the unique values for the "key" field, then iterates over those keys and fetches all objects matching that "key". In the second fetch, you might want to fetch NSManagedObjects instead of dictionaries, depending on what you're going to do.

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]);
}

This yields

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;
    }
)

I also tried

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"]);

which might be closer to what you want:

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
)

这篇关于可以将group_concat与coredata一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆