LoginSignup
2

More than 5 years have passed since last update.

[AWS,iOS]AWSDynamoDBで比較演算を用いて、条件付き書き込みを行う例

Last updated at Posted at 2015-06-02

前提

  • 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;

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2