Swift DynamoDB映射器发送空值 [英] Swift DynamoDB Mapper Sending Empty Values
问题描述
我正在尝试通过iOS Swift SDK使用DynamoDB。我将Cognito与Facebook一起用作外部身份提供商。 Cognito运作良好-我已经测试了使用者同步处理,而且运作正常,因此我相信我已经设定了验证。这是我设置SDK的方式(我的代码中有我的身份池的实际值):
I'm trying to use DynamoDB using the iOS Swift SDK. I'm using Cognito with Facebook as an external identity provider. Cognito is working fine - I've tested user sync and it works OK, so I believe I have the authentication set up. Here's how I'm setting up the SDK (I have the actual values of my identity pool in my code):
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
identityPoolId:"<my-identity-pool-id>", identityProviderManager: FacebookProvider())
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
这是我的DynamoDB映射的类:
And here's my DynamoDB mapped class:
import Foundation
import AWSDynamoDB
class SavedItem : AWSDynamoDBObjectModel, AWSDynamoDBModeling {
var userId : Int?
var timestamp : Int?
class func dynamoDBTableName() -> String {
return "my-table"
}
class func hashKeyAttribute() -> String {
return "userId"
}
class func rangeKeyAttribute() -> String {
return "timestamp"
}
}
I已经验证我的代码具有正确的表和属性名称,并且表上的哈希键和范围键值相同,包括区分大小写,并且我的 SavedItem
类。
I've verified that my code has the correct table and attribute names and that the hash key and range key values on the table are identical, including case sensitivity, with the fields in my SavedItem
class.
这是我实例化映射器的方式:
Here's how I'm instantiating the mapper:
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let savedItem = SavedItem()
savedItem?.userId = 1
savedItem?.timestamp = 2
dynamoDBObjectMapper.save(savedItem!).continueWith(block: { (task:AWSTask<AnyObject>!) -> Any? in
if let error = task.error as? NSError {
print("The request failed. Error: \(error)")
} else {
print("Save callback executing")
}
return nil
})
AWS Documentation示例中的代码或多或少是直接的。但是,执行该代码后,我会在控制台中看到以下内容:
That code's more or less straight out from the AWS Documentation example. But, here's what I get back in the console when that code executes:
Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" UserInfo={__type=com.amazon.coral.validate#ValidationException, message=Supplied AttributeValue is empty, must contain exactly one of the supported datatypes}
我碰到控制台登录调试,看来映射器没有从 SavedItem
发送任何属性宾语。以下是保存
请求正文的控制台中的内容:
I bumped console logging up to debug, and it looks like the mapper is not sending any attributes from the SavedItem
object. Here's what's in the console for the save
request body:
Request body:
{"Key":{"userId":{},"timestamp":{}},"TableName":"my-table","AttributeUpdates":{}}
是否知道为什么值未包含在保存请求正文中?
Any idea why the values are not getting included in the save request body?
在iOS 11的Swift 4中使用aws-sdk-ios v2.6.1。
Using aws-sdk-ios v2.6.1 on in Swift 4 on iOS 11.
推荐答案
问题似乎出在 userId
和 timestamp
的类型。将它们从 Int
更改为 NSNumber
可以解决此问题。
The problem seems to be the type of userId
and timestamp
. Changing them from Int
to NSNumber
fixed the problem.
这篇关于Swift DynamoDB映射器发送空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!