Incrementing Number Property in AWS DynamoDB Objec

2019-02-20 00:19发布

问题:

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.