2016-10-29 80 views
0

我需要在覈心數據傳輸的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:'

+0

核心數據不是SQL數據庫。這是一個對象持久化系統,可以*使用SQLite作爲對象存儲。 – Paulw11

回答

0

首先,如果沒有看到您的數據模型,我們實際上無法幫助您調試您的查詢。

你的錯誤:

Invalid keypath expression ((), name quadKeyMask, isOptional 1, isTransient 0, entity (null), renamingIdentifier quadKeyMask, validation predicates (), warnings (), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch: 

指示該鍵值路徑的解釋就不能按照你的數據模型。 IOW,你的實體或表達中有錯誤。

核心數據中重寫SQL查詢的關鍵是瞭解它們是非常不同的語言。核心數據謂詞使用過濾條件設置選擇操作。雖然這看起來與SQL聲明式語言相似,但它是不同的。概念模型是不同的。

我的建議:用小塊重寫你的查詢。就像你會調試任何其他問題一樣。使小塊工作;然後使您的表達式的小組合工作。分而治之。