核心数据组通过获取所有行业问题

时间:2015-08-15 06:41:16

标签: ios objective-c core-data ios7 ios8

我有一张桌子

DB_SMS 
----------------------------------------
contactId | messageText | to | createdAt

我想为每个contactId获取最近创建的记录。例如:

contactId | messageText | to | createdAt
        1 |       msg01 |  a | 2015-04-20
        1 |       msg02 |  b | 2015-04-21
        2 |       msg03 |  c | 2015-04-20
        3 |       msg04 |  d | 2015-04-20
        3 |       msg05 |  w | 2015-04-22

必填结果

contactId | messageText | to | createdAt
        1 |       msg02 |  b | 2015-04-21
        2 |       msg03 |  c | 2015-04-20
        3 |       msg05 |  w | 2015-04-22

我正在使用以下代码

NSFetchRequest * theRequest = [NSFetchRequest fetchRequestWithEntityName:@"DB_SMS"];

[theRequest setResultType:NSDictionaryResultType];

[theRequest setPropertiesToFetch:@[@"contactId",@"messageText",@"to",@"createdAt"]];

[theRequest setPropertiesToGroupBy:@[@"contactId"]];

[theRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]]];

NSArray * theArray = [theDbHandler.managedObjectContext executeFetchRequest:theRequest error:nil];

我正在追踪异常

SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions 

PLZ建议一些解决方案。

0 个答案:

没有答案