前提
- iOS(Objective-C)
- AWS MobileSDK
- AWS DynamoDB
「前回値がXだったら更新を行う」という例は、DynamoDBのドキュメント Conditional Writes Using the Low-Level Clientにありますが、 「前回値がX以下だったら更新を行う」例が見つけられなかったので記事にしました。<,<=,>,>=のように比較を行う場合は、AWSDynamoDBExpectedAttributeValueの使い方をちょっと変える必要があるようです。
「前回値がXだったら更新を行う」場合
// (ドキュメント Conditional Writes Using the Low-Level Clientより抜粋)
// 古いPriceが999なら1199に変更する。
AWSDynamoDBAttributeValue *oldPrice = [AWSDynamoDBAttributeValue new];
oldPrice.N = @"999";
AWSDynamoDBExpectedAttributeValue *expectedValue = [AWSDynamoDBExpectedAttributeValue new];
expectedValue.value = oldPrice;
AWSDynamoDBAttributeValue *newPrice = [AWSDynamoDBAttributeValue new];
newPrice.N = @"1199";
updateInput.attributeUpdates = @{@"Price": valueUpdate};
updateInput.expected = @{@"Price": expectedValue};
updateInput.returnValues = AWSDynamoDBReturnValueUpdatedNew;
「前回値がX未満だったら更新を行う」場合
AWSDynamoDBExpectedAttributeValue.valueの代わりに、AWSDynamoDBExpectedAttributeValue.attributeValueListを利用します。
参考:AWSDynamoDBExpectedAttributeValue class reference
// 「前回値がnewPrice未満だったら更新を行う」例(Conditional Writes Using the Low-Level Clientより抜粋)
AWSDynamoDBAttributeValue *newPrice = [AWSDynamoDBAttributeValue new];
newPrice.N = @"1199";
AWSDynamoDBExpectedAttributeValue *expectedValue = [AWSDynamoDBExpectedAttributeValue new];
expectedValue.comparisonOperator = AWSDynamoDBComparisonOperatorLT;
expectedValue.attributeValueList = @[newPrice];
updateInput.attributeUpdates = @{@"Price": valueUpdate};
updateInput.expected = @{@"Price": expectedValue};
updateInput.returnValues = AWSDynamoDBReturnValueUpdatedNew;