简体   繁体   English

更新 DynamoDB (Swift) 中特定项目的属性

[英]Update an attribute on a specific item in DynamoDB (Swift)

I want to fetch a specific item from my DynamoDB table, defined by its unique partition key, a string that I've passed into the method.我想从我的 DynamoDB 表中获取一个特定的项目,由它的唯一分区键定义,这是一个我传递给方法的字符串。 I want to then change one of the string attributes from "false" to "true".然后我想将其中一个字符串属性从“false”更改为“true”。

This is the code I have so far这是我到目前为止的代码

var updatedValue: AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue()
updatedValue.S = "true"

var updateInput: AWSDynamoDBUpdateItemInput = AWSDynamoDBUpdateItemInput()
updateInput.tableName = "knot-listings"
updateInput.key = ["ID": self.ID]
var valueUpdate: AWSDynamoDBAttributeValueUpdate = AWSDynamoDBAttributeValueUpdate()
valueUpdate.value = updatedValue
valueUpdate.action = AWSDynamoDBAttributeAction.Put
updateInput.attributeUpdates = ["Updated": valueUpdate]
updateInput.returnValues = AWSDynamoDBReturnValue.UpdatedNew

self.dynamoDB.updateItem(updateInput).waitUntilFinished()

self.ID refers to the unique identifier string. self.ID指的是唯一标识符字符串。 I want to select an attribute called "sold" and change its value to "true".我想选择一个名为“sold”的属性并将其值更改为“true”。

I actually used your code as a guide.我实际上使用了您的代码作为指南。

Don't use不要使用

self.dynamoDB.updateItem(updateInput).waitUntilFinished()

But Instead但反而

dynamoDB.updateItem(updateInput).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
    if (task.error == nil) {


    }

    return nil

})

Altogether

    var updatedValue: AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue()
    updatedValue.S = "true"

    var updateInput: AWSDynamoDBUpdateItemInput = AWSDynamoDBUpdateItemInput()
    updateInput.tableName = "knot-listings"
    updateInput.key = ["ID": self.ID]
    var valueUpdate: AWSDynamoDBAttributeValueUpdate = AWSDynamoDBAttributeValueUpdate()
    valueUpdate.value = updatedValue
    valueUpdate.action = AWSDynamoDBAttributeAction.Put
    updateInput.attributeUpdates = ["attributeName": newAttributeValue]
    updateInput.returnValues = AWSDynamoDBReturnValue.UpdatedNew


    dynamoDB.updateItem(updateInput).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
    if (task.error == nil) { 
    }

    return nil

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM