2
我試圖使用低級API將字典保存到DynamoDB表字段。我無法弄清楚如何用對象映射器來做到這一點。在AWS iOS文檔中沒有這樣的例子,我試圖研究和實現相同主題的Java/.NET示例。如何使用Swift將字典(映射對象)保存到DynamoDB
我想僅使用updateExpression更新行中的字典字段。
我偶然發現了這個問題,同時尋找答案,但它並沒有幫助:Best way to make Amazon AWS DynamoDB queries using Swift?
這裏的更新dynamoDB表功能:
func saveMapToDatabase(hashKey:Int, rangeKey:Double, myDict:[Int:Double]){
let nsDict:NSDictionary = NSDictionary(dictionary: myDict)
var dictAsAwsValue = AWSDynamoDBAttributeValue();dictAsAwsValue.M = nsDict as [NSObject : AnyObject]
let updateInput:AWSDynamoDBUpdateItemInput = AWSDynamoDBUpdateItemInput()
let hashKeyValue:AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue();hashKeyValue.N = String(hashKey)
let rangeKeyValue:AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue(); rangeKeyValue.N = String(stringInterpolationSegment: rangeKey)
updateInput.tableName = "my_table_name"
updateInput.key = ["db_hash_key" :hashKeyValue, "db_range_key":rangeKeyValue]
//How I usually do low-level update:
//let valueUpdate:AWSDynamoDBAttributeValueUpdate = AWSDynamoDBAttributeValueUpdate()
//valueUpdate.value = dictAsAwsValue
//valueUpdate.action = AWSDynamoDBAttributeAction.Put
//updateInput.attributeUpdates = ["db_dictionary_field":valueUpdate]
//Using the recommended way: updateExpression
updateInput.expressionAttributeValues = ["dictionary_value":dictAsAwsValue]
updateInput.updateExpression = "SET db_dictionary_field = :dictionary_value"
self.dynamoDB.updateItem(updateInput).continueWithBlock{(task:BFTask!)->AnyObject! in
//do some debug stuff
println(updateInput.aws_properties())
println(task.description)
return nil
}
}
你可以看看[對象映射器測試(https://github.com/aws/aws-sdk-ios/blob/master/AWSDynamoDBTests/AWSDynamoDBObjectMapperTests .m#L550)的一些示例代碼。 –