Dynamo DB无法保存数据

时间:2018-07-15 01:47:19

标签: ios swift amazon-web-services amazon-dynamodb

我尝试了多种方法将项目保存到Dynamo DB,但是我仍然遇到相同的错误:

UserInfo={__type=com.amazon.coral.validate#ValidationException, message=Supplied AttributeValue is empty, must contain exactly one of the supported datatypes}

这是我的模型文件:

//  ImageTable.swift
//  MySampleApp
//
//
// Copyright 2018 Amazon.com, Inc. or its affiliates (Amazon). All Rights Reserved.
//
// Code generated by AWS Mobile Hub. Amazon gives unlimited permission to 
// copy, distribute and modify it.
//
// Source code generated from template: aws-my-sample-app-ios-swift v0.21
//

import Foundation
import UIKit
import AWSDynamoDB

class ImageTable: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    var _imageID: String?
    var _kValue: NSNumber?
    var _latitude: NSNumber?
    var _longitude: NSNumber?

    class func dynamoDBTableName() -> String {

        return "sendit-mobilehub-1219842108-ImageTable"
    }

    class func hashKeyAttribute() -> String {

        return "_imageID"
    }

    class func rangeKeyAttribute() -> String {

        return "_kValue"
    }

    override class func jsonKeyPathsByPropertyKey() -> [AnyHashable: Any] {
        return [
           "_imageID" : "imageID",
           "_kValue" : "k-value",
           "_latitude" : "latitude",
           "_longitude" : "longitude",
        ]
    }
}

这是我用来保存数据的代码:

let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()

let imageTable: ImageTable = ImageTable()

imageTable._imageID = AWSIdentityManager.default().identityId
print(imageTable._imageID)
imageTable._kValue = NSNumber(value: 1)
imageTable._latitude = NSNumber(value: 1)
imageTable._longitude = NSNumber(value: 1)

//Save a new item
dynamoDbObjectMapper.save(imageTable, completionHandler: {
    (error: Error?) -> Void in

    if let error = error {
        NSLog("Amazon DynamoDB Save Error: \(error)")
        return
    }
    NSLog("An item was saved.")
})

我已经尝试过的步骤:

  • 我尝试将@objcMembers属性添加到类声明上方的模型文件中,但未做任何更改。
  • 我确保在创建ImageTable对象后检查是否已实例化了对象中的所有变量。

谢谢!

1 个答案:

答案 0 :(得分:1)

DynamoDB的属性限制:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-attributes。属性值不能为空字符串或空集(字符串集,数字集或二进制集)。但是,允许使用空的列表和地图。

您可以检查表中的所有四个字段是否都具有有效的非空值?我的怀疑是AWSIdentityManager.default().identityId为空。您必须在iOS应用中使用AWS Auth SDK设置身份验证,以便此身份ID是有效且非空的字符串。参考:https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-sign-in.html