ios - AWS DynamoDB Objective C 中递增 Number 属性

标签 ios amazon-web-services amazon-dynamodb updates increment

我正在努力增加已保存在 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/

相关文章:

ios - 如何删除 tableview 标题和第一个单元格之间的空间?

objective-c - 在 Objective C 中使用未声明的标识符

ios - 避免检测终点导航

amazon-web-services - AttributeDefinitions 和 KeySchema 中的属性集必须在 Resources/DynamoDB/Properties 中匹配

amazon-dynamodb - DynamoDB 读写

amazon-web-services - Appsync Resolver UpdateItem 忽略空参数?

ios - 获取 NSArray 中字符串属性以特定字母开头的所有对象

amazon-web-services - 如何更改 Elastic Beanstalk 中的 http nginx 超时

amazon-web-services - 当多个模式匹配时,API Gateway 指向哪里?

amazon-web-services - 弹性 beantalk 上的 Golang 应用程序似乎正在接收双重编码请求