Swift 4

时间:2018-04-09 20:08:50

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

我正在尝试更新dynamoDB noSQL数据库中的项目。由于没有快速的文档,因此在swift中实现这一点时遇到了一些麻烦。

我能够在数据库中成功创建一个项目,更新项目似乎是另一个怪物。

Swift Code:

var updatedValue: AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue()
updatedValue.s = self.UserID

let dynamo: AWSDynamoDB = AWSDynamoDB()

let AddToHistory =  Users()
AddToHistory?._campany = self.CompanyTextBox.text!
AddToHistory?._personalSite = self.PersonalTitleTextBox.text!
AddToHistory?._facebook = self.FacebookTextBox.text!
AddToHistory?._linkedIn = self.LinkedInTextBox.text!
AddToHistory?._title = self.TitleTextBox.text!
AddToHistory?._bio = self.BioTextBox.text!

let updateInput: AWSDynamoDBUpdateItemInput = AWSDynamoDBUpdateItemInput()
updateInput.tableName = "myTableName"
updateInput.key = ["_userId": updatedValue]

    let updatedCompany = AWSDynamoDBAttributeValue()
    updatedCompany?.s = AddToHistory?._campany

    let updatedFacebook = AWSDynamoDBAttributeValue()
    updatedFacebook?.s = AddToHistory?._facebook

    let updatedLinkedIn = AWSDynamoDBAttributeValue()
    updatedLinkedIn?.s = AddToHistory?._linkedIn

    let updatedPersonalSite = AWSDynamoDBAttributeValue()
    updatedPersonalSite?.s = AddToHistory?._personalSite

    let updatedTitle = AWSDynamoDBAttributeValue()
    updatedTitle?.s = AddToHistory?._title

    let updatedBio = AWSDynamoDBAttributeValue()
    updatedBio?.s = AddToHistory?._bio

    updateInput.expressionAttributeValues = [
        "_campany" : updatedCompany!,
        "_facebook" : updatedFacebook!,
        "_linkedIn" : updatedLinkedIn!,
        "_personalSite" : updatedPersonalSite!,
        "_title" : updatedTitle!,
        "_bio" : updatedBio!,
    ]

    updateInput.returnValues = AWSDynamoDBReturnValue.updatedNew


    dynamo.updateItem(updateInput).continueOnSuccessWith(block: { (task:AWSTask!) -> AnyObject! in
            if (task.error == nil) {
            }

             return nil
        }
    )

在编辑器中没有收到任何警告或错误,但是当我运行应用程序并按下运行此代码的按钮时,我会抛出此异常:

  

由于未捕获的异常而终止应用   ' NSInternalInconsistencyException',原因:' - init无效   初始化。请改用+ defaultDynamoDB+ DynamoDBForKey:。'

不确定我在这里缺少什么,它必须与我初始化dynamoDB对象的方式有关。尝试访问init的默认方法,但没有这样的方法。 :(

非常感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:1)

感谢 Jake.lange的评论,我意识到我可以使用我用来创建项目的对象映射器来更新它们。下面是代码,其他人遇到了这个问题:)

   //db connection mapper
        let objectMapper = AWSDynamoDBObjectMapper.default()

        //new instancer of User class
        let itemToUpdate:CheckaraUsers = CheckaraUsers()

        //populate
        itemToUpdate._userId = UserID
        itemToUpdate._firstName = FirstName
        itemToUpdate._lastName = LastName
        itemToUpdate._campany = AddToHistory?._campany
        itemToUpdate._facebook = AddToHistory?._facebook
        itemToUpdate._linkedIn = AddToHistory?._linkedIn
        itemToUpdate._personalSite = AddToHistory?._personalSite
        itemToUpdate._title = AddToHistory?._title
        itemToUpdate._bio = AddToHistory?._bio

        //save to dynamoDB
        objectMapper.save(itemToUpdate, completionHandler:{(error: Error?) -> Void in
            if let error = error {
                print("Amazon DynamoDB Save Error: \(error)")
            }

            print("Saved Information!!!")
        })