我需要在覈心數據傳輸的SQL查詢:iOS CoreData。通過與集團掩蓋
SELECT `quadkey`, MIN(id) AS id, COUNT(id) AS count, AVG(longitude) AS longitude, AVG(latitude) AS latitude
FROM marker
GROUP BY (quadkey & 15499683151872);
我的代碼:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"GBUserRoute"];
NSExpressionDescription *countExpressionDescription = [self expressionForFunction:@"count:" arguments:@[[NSExpression expressionForKeyPath:@"routeId"]] resultName:@"count" resultType:NSDoubleAttributeType];
NSExpressionDescription *minIdExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"routeId"]] resultName:@"minId" resultType:NSDoubleAttributeType];
NSExpressionDescription *averageLatitudeExpressionDescription = [self expressionForFunction:@"average:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"latitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *averageLongitudeExpressionDescription = [self expressionForFunction:@"average:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"longitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *minLongitudeExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"minLongitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *minLatitudeExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"minLatitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *maxLongitudeExpressionDescription = [self expressionForFunction:@"max:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"maxLongitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *maxLatitudeExpressionDescription = [self expressionForFunction:@"max:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"maxLatitude" resultType:NSDoubleAttributeType];
NSExpressionDescription *groupExpressionDescription = [self expressionForFunction:@"bitwiseAnd:with:" arguments:@[[NSExpression expressionForKeyPath:@"quadKey"], [NSExpression expressionForConstantValue:@(15499683151872)]] resultName:@"quadKeyMask" resultType:NSInteger64AttributeType];
[fetchRequest setPropertiesToFetch:@[groupExpressionDescription, countExpressionDescription, minIdExpressionDescription, averageLongitudeExpressionDescription, averageLatitudeExpressionDescription, minLongitudeExpressionDescription, minLatitudeExpressionDescription, maxLongitudeExpressionDescription, maxLatitudeExpressionDescription]];
[fetchRequest setPropertiesToGroupBy:@[groupExpressionDescription]];
[fetchRequest setResultType:NSDictionaryResultType];
+ (NSExpressionDescription *)expressionForFunction:(NSString *)function arguments:(NSArray<NSExpression *> *)arguments resultName:(NSString *)resultName resultType:(NSAttributeType)resultType {
NSExpression *expression = [NSExpression expressionForFunction: function
arguments: arguments];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: resultName];
[expressionDescription setExpression: expression];
[expressionDescription setExpressionResultType: resultType];
return expressionDescription;
}
運行此代碼,我得到以下錯誤:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath expression ((), name quadKeyMask, isOptional 1, isTransient 0, entity (null), renamingIdentifier quadKeyMask, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch:'
核心數據不是SQL數據庫。這是一個對象持久化系統,可以*使用SQLite作爲對象存儲。 – Paulw11