Swift DynamoDB Mapper Sending Empty Values

2019-08-26 13:58发布

问题:

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

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'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
})

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}

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?

Using aws-sdk-ios v2.6.1 on in Swift 4 on iOS 11.

回答1:

The problem seems to be the type of userId and timestamp. Changing them from Int to NSNumber fixed the problem.



回答2:

Yes i faced this same issue. and after struggling for 3 days.i found that AWSDynamoDBObjectModel not support in swift 4. please try in swift 3 version. You will get success.