EmptyStack
EmptyStack

Reputation: 51374

Amazon Dynamo DB - Scan & Update table records - iOS SDK

Is there an option to scan & update a record in a DynamoDB table using a single API. Consider I have list of items in a Items table, with the fields (ItemID, ItemName, ItemAssigned). ItemID is the Hash key of the table. Items IDs are named like , Item1, Item2, Item3 etc., The users will not know what are the items are in the table. So, in the app, if a user taps "Get an item" button, then a random item will be fetched and assigned to him. Then ItemAssigned is set to YES. The Items tabe Then the item will not be assigned to any other user. This is what happens.

Steps:
1. Taps "Get an item"
2. Scan the first item from the Items table, where ItemAssigned = NO
3. Show the item in the home screen
4. Update the item in the Items table to set ItemAssigned = YES

This works good, if one user is trying to get the item at a time. The problem occurs when two users A & B are trying to get an item at the same time, and the same item is assigned to both the users.

So first A fetches the item. Before the Items table is updated to set ItemAssigned = YES, B also fetches the same item. Now both A & B's home screens show the same item. This is wrong.

The only way is to Scan & Update the Items table using a single API. Is there any possibility to do this in Amazon DynamoDB?

Thanks.

Upvotes: 1

Views: 1708

Answers (1)

Bob Kinney
Bob Kinney

Reputation: 9020

Unfortunately there is no API that scans and updates in a single operation. You may want to look into conditional PUT item as this will allow you to implement a checkout like feature such that you can set "ItemAssigned = YES" only if "ItemAssigned = NO".

Your application code will need to handle the exception gracefully when dealing with a scan result, but will allow for the workflow you're requesting of only allowing one user to see the item.

API docs for PUT item

Upvotes: 5

Related Questions