Search code examples
swiftamazon-dynamodbaws-mobilehub

Can't retrieve data from DynamoDB using MobileHub


I can succeed to retrieve data from a DynamoDB table.

func myRequest(completionHandler: (response: AWSDynamoDBObjectModel?, error: NSError?) -> Void) {

let objectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()

objectMapper.load(myModel.self,
                          hashKey: AWSIdentityManager.defaultIdentityManager().identityId!,
                          rangeKey: nil,
                          completionHandler: {(response: AWSDynamoDBObjectModel?, error: NSError?) -> Void in
            dispatch_async(dispatch_get_main_queue(), {
                completionHandler(response: response, error: error)
            })
        })


    }

Completion handler complain response is nil but log indicates there is a response which content is the expected one.

So it seems the problem occurs on mapping JSON response to table' model myModel.

To obtain myModel I simply took sample app from Mobile Hub and replace existing fields by the one I use.


Solution

  • Problem solved: model properties contained capital letters. Removing them make the process work.

    Hereafter is presented the non working code. In the project field name on DynamoDB must begin with a capital letter. I then created properties following this rule, with an added prefixing underscore.

    class MyModel: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
    
    var _UserId: String?
    var _ArticleId: String?
    
    
    class func dynamoDBTableName() -> String {
    
        return "myTable"
    }
    
    class func hashKeyAttribute() -> String {
    
        return "_UserId"
    }
    
    class func rangeKeyAttribute() -> String {
    
        return "_ArticleId"
    }
    
    override class func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject] {
        return [
               "_UserId" : "UserId",
               "_ArticleId" : "ArticleId",
    
        ]
    }
    

    }

    An this is the working code:

    class MyModel: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
    
    var _userId: String?
    var _articleId: String?
    
    
    class func dynamoDBTableName() -> String {
    
        return "myTable"
    }
    
    class func hashKeyAttribute() -> String {
    
        return "_userId"
    }
    
    class func rangeKeyAttribute() -> String {
    
        return "_articleId"
    }
    
    override class func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject] {
        return [
               "_userId" : "UserId",
               "_articleId" : "ArticleId",
    
        ]
    }
    

    }

    Strange, isn't it?