我正在使用 nsexpression 对多个属性进行分组和计数。提取结果在 ios 6 和 ios 7 下工作正常,但在 ios 5 下不起作用。
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“使用 GROUP BY 组件的查询中的 SELECT 子句只能包含在 GROUP BY 或聚合函数中命名的属性(startDateTime 不在 GROUP BY 中)”*First throw call stack: (0x3689022 0x309bcd6 0x13357d9 0x133543e 0x13351f0 0x1334fc3 0x1334619 0x13340b0 0x133345d 0x13312fd 0x138432b 0x13841fc 0x1384051 0x3285951 0x3285e00 0x1383ff5 0x1383e6d 0x13312fd 0x144043f 0x3a55f 0x38fea 0x22c5a1e 0x2430323 0x3a796 0xc961 0x1b01c6 0xc572 0xc42a 0x1b0684 0x215b34 0x19dff2 0x215a8d 0x1b05cd 0xc142 0xf5c5 0x1b0684 0x215b34 0x19dff2 0x215a8d 0x1b05cd 0xd722 0x384b 0x21fc386 0x21fd274 0x220c183 0x220cc38 0x2200634 0x5177ef5 0x365d195 0x35c1ff2 0x35c08da 0x35bfd84 0x35bfc9b 0x21fcc65 0x21fe626 0x29ad 0x2915 0x1) 终止调用异常
代码如下:
NSPropertyDescription *propDesc = [properties objectForKey:@"partyId"];
NSExpression *partyid = [NSExpression expressionForKeyPath:@"partyId"];
NSExpression *partyIdCount = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:partyid]];
NSExpressionDescription *countPartyId = [[NSExpressionDescription alloc] init];
[countPartyId setExpression:partyIdCount];
[countPartyId setExpressionResultType:NSInteger64AttributeType];
[countPartyId setName:@"countResultPartyId"];
NSExpression *startDateTime = [NSExpression expressionForKeyPath:@"startDateTime"];
NSExpression *latestDateTime = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:startDateTime]];
NSExpressionDescription *dateTimePropertyDescription = [[NSExpressionDescription alloc] init];
[callDateTimePropertyDescription setExpression:latestDateTime];
[callDateTimePropertyDescription setExpressionResultType:NSDateAttributeType];
[callDateTimePropertyDescription setName:@"latestDateTime"];
NSPropertyDescription *sortDateGroup = [properties objectForKey:@"sortDate"];
NSPropertyDescription *userId = [properties objectForKey:@"userId"];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:propDesc,sortDateGroup,userId, nil]];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:propDesc,dateTimePropertyDescription, sortDateGroup,userId,countPartyId, nil]];
[fetchRequest setResultType:NSDictionaryResultType];
看起来它也想让我按时间分组startDateTime
。但是分组不起作用,因为可能有多个 startDateTime 并且我需要它的最新“最大”时间。令人惊讶的是,它可以在 ios 6 和 7 下运行——有人知道如何做到这一点吗?谢谢你。