我正在努力增加已保存在 DynamoDB 上的表中的项目的数字属性值,我的代码当前是:
AWSDynamoDBUpdateItemInput *updateItemInput = [AWSDynamoDBUpdateItemInput new];
updateItemInput.tableName = @"Table";
updateItemInput.key= @{
@"KeyPropertyName":@"KeyValue"
};
updateItemInput.updateExpression = @"SET(counter = counter + :val)";
updateItemInput.expressionAttributeValues =@{
@":val":@1
};
AWSDynamoDB *dynamoDB = [AWSDynamoDB defaultDynamoDB];
[[dynamoDB updateItem:updateItemInput]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
//Do something with result.
}
return nil;
}];
当我不注释掉 updateExpression 和 expressionAttributeValues 时,我的应用程序总是崩溃。当我创建项目类型的空实例时,我可以到达该 block ,而当我创建 AWSDynamoDBAttributeValue 实例以传入 key 时,我会得到奇怪的结果。有什么建议么?我的 updateExpression 是否也写得不正确?
我还将在另一个对象的数组/列表和字典/映射属性中添加更新和删除项目。这会有什么不同?
最佳答案
有几件事。 key
定义如下:
@property (nonatomic, strong) NSDictionary<NSString *, AWSDynamoDBAttributeValue *> * _Nullable key;
您需要将 @"KeyValue"
替换为 AWSDynamoDBAttributeValue
实例。
expressionAttributeValues
定义如下:
@property (nonatomic, strong) NSDictionary<NSString *, AWSDynamoDBAttributeValue *> * _Nullable expressionAttributeValues;
您需要将 @1
替换为 AWSDynamoDBAttributeValue
实例。
确保使用最新版本的适用于 iOS 的 AWS 开发工具包,以便您可以在头文件中看到泛型注释。它可以帮助您构建正确的请求对象。
关于ios - AWS DynamoDB Objective C 中递增 Number 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488229/