Michel
Michel

Reputation: 11755

How to remove an item from a DynamoDB table ? (in swift)

Here is some working code, extracting one item from a DynamoDB table:

    let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default(),
    scanExpression = AWSDynamoDBScanExpression()
    scanExpression.limit = 1
    dynamoDbObjectMapper.scan(MyTable.self, expression: scanExpression) {
        [weak self] (output: AWSDynamoDBPaginatedOutput?, error: Error?) in
        if error != nil {
            print("The request failed. Error: \(String(describing: error))")
        }

        if output != nil {
            currentItem = output!.items[0]
            .. Do useful things with the output ..
            .........
            // Now I want to erase currentItem from the DynamoDB table!
        }
    }

I would like to erase currentItem from the table, once this code is run. What is the best way to do it?

I guess that must not be too difficult, but I can't find the solution (meaning a swift example) by searching the net.

Upvotes: 1

Views: 225

Answers (1)

Michel
Michel

Reputation: 11755

Just in case someone else hits the same question.

Here is how I solved it:

    let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
    dynamoDbObjectMapper.remove(objectToDelete).continueWith(block: {
        (task:AWSTask<AnyObject>!) -> Any? in
        if let error = task.error {
            print("Error in \(#function):\n\(error)")
        }

        return nil
    })

Upvotes: 2

Related Questions