ios - 如何在核心数据中应用 group by 子句

标签 ios iphone objective-c core-data

我正在实现 tableview,我想在 tableview 部分显示类名,我正在尝试获取 使用核心数据实现的数据库中的类值,我想使用类名上的 group by 子句获取数据这是我的代码

   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    //create managed object context
    NSManagedObjectContext *context = [appDelegate managedObjectContext];


    NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Boking_Detail"];
    NSEntityDescription* entity = [NSEntityDescription entityForName:@"Boking_Detail"
                                              inManagedObjectContext:context];
    NSAttributeDescription* clsName = [entity.attributesByName objectForKey:@"classname"];
    NSAttributeDescription* clsDate = [entity.attributesByName objectForKey:@"classdate"];
    NSAttributeDescription* startTime = [entity.attributesByName objectForKey:@"starttime"];
    NSAttributeDescription* endTime = [entity.attributesByName objectForKey:@"endtime"];

    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName: @"count"];
   // [expressionDescription setExpression: countExpression];
    [expressionDescription setExpressionResultType: NSInteger32AttributeType];
    [fetch setPropertiesToFetch:[NSArray arrayWithObjects:clsName,clsDate,startTime,endTime, expressionDescription, nil]];
    [fetch setPropertiesToGroupBy:[NSArray arrayWithObject:clsName]];
    [fetch setResultType:NSDictionaryResultType];
    NSError* error = nil;
    NSArray *results = [context executeFetchRequest:fetch  error:&error];

    NSLog(@"result array count %i",results.count);
     NSLog(@"result array values %@",results);

我收到这个错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((<NSAttributeDescription: 0xc288680>), name classdate, isOptional 1, isTransient 0, entity Boking_Detail, renamingIdentifier classdate, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null) is not in the GROUP BY)'
*** First throw call stack:

最佳答案

您还必须将 propertiesToFetch 中的其他属性添加到 group by 子句中,表达式除外:

[fetch setPropertiesToGroupBy:[NSArray arrayWithObjects:clsName, clsDate, startTime, endTime, nil]];

关于ios - 如何在核心数据中应用 group by 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995405/

相关文章:

ios - 如何在单击同一个 UIbutton 时更改 UIButton 背景图像?

ios - GPUImage2 无法实时循环过滤器

iphone - UILabel - 在 iPhone 中以编程方式设置字体 - 字体

在空指针异常的情况下,Java 与 Objective C

objective-c - 如何删除 ASIHTTPRequest 中 http 摘要身份验证使用的凭据

objective-c - 在 UIWebView 和 UIViewController 之间共享数据

ios - 通过viewController点击rootViewController

ios - 使用谷歌地图的 iOS 上的转弯 GPS 导航应用程序?

iphone - Apple Appstore 申请提交要求

ios - 自定义 segue 到不同的 Storyboard