Incrementing Number Property in AWS DynamoDB Objective C

孤人 提交于 2019-12-01 08:17:31

问题


I am struggling with incrementing a number property value of an item already saved in my table on DynamoDB my code currently is:

        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;
         }];

My app always crashes when I do not comment out the updateExpression and expressionAttributeValues. I can reach the block when I create an empty instance of my item type and get strange results when I create an instance of AWSDynamoDBAttributeValue to pass in the key. Any suggestions? Am I also improperly writing my updateExpression?

I am also going to be adding updating and deleting items in array/list and dictionary/map properties on another object. How would this differ?


回答1:


There are a couple of things. key is defined as follows:

@property (nonatomic, strong) NSDictionary<NSString *, AWSDynamoDBAttributeValue *> * _Nullable key;

You need to replace @"KeyValue" with an instance of AWSDynamoDBAttributeValue.

expressionAttributeValues is defined as follows:

@property (nonatomic, strong) NSDictionary<NSString *, AWSDynamoDBAttributeValue *> * _Nullable expressionAttributeValues;

You need to replace @1 with an instance of AWSDynamoDBAttributeValue.

Make sure to use the latest version of the AWS SDK for iOS so that you can see the generics annotations in the header file. It helps you construct the proper request object.



来源:https://stackoverflow.com/questions/36488229/incrementing-number-property-in-aws-dynamodb-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!