如何在核心数据中逐组应用
我正在实现tableview,我想在tableview部分显示类名称,我正在尝试使用核心数据实现的数据库中获取类的值,我想要使用classname上的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:
除了以下expression式之外,还必须将propertiesToFetch
中的其他属性添加到group by子句中:
[fetch setPropertiesToGroupBy:[NSArray arrayWithObjects:clsName, clsDate, startTime, endTime, nil]];
- Swift无法在Xcodetesting中testing核心数据?
- 如何编辑从iOS5中的妈妈加载的NSManagedObjectModel
- 核心数据对象的生命周期和编译器优化
- NSPredicate与AND和OR问题
- 访问XCTestCase类中的应用程序委托对象导致misaligned_stack_error_ swift
- 什么是NSManagedObjectContext的performBlock:用于?
- 核心数据pipe理对象上下文devisebuild议
- Swift CoreData:错误:无法调用NSManagedObject类NSManagedObject上的指定初始值设定项
- CoreDataasynchronous获取导致并发debugging器错误