0

Let me preface this question by saying that I'm very new to coding with Swift and in iOS in general. I've got some experience in Java/Android and am starting to work with iOS as well now.

I need to query a dynamodb table with enough data in it that Amazon paginates the results (I think the limit is 100kb). Using the limited examples for AWS/Swift I am able to query that table but only successfully retrieve the first page of data. My question is how to get that 2nd, 3rd, etc page of data. See code below

let queryExpression = AWSDynamoDBQueryExpression()
        queryExpression.keyConditionExpression = "venue_event = :ev"
        queryExpression.expressionAttributeValues = [":ev" : "event"]

        dynamoDBObjectMapper.query(Event.self, expression: queryExpression).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {(task:AWSTask!) -> AnyObject! in
            let results = task.result as! AWSDynamoDBPaginatedOutput 
            for r in results.items{
                print (r)
            }
            return nil
        })

I've noticed that 'results' has a lastEvaluatedKey variable and loadNextPage method. However I can't seem to get either to give me the function I'm looking for

Thanks in advance for the help

1 Answer 1

1

Simply:

var paginatedOutput: AWSDynamoDBPaginatedOutput?
...
    self.paginatedOutput = task.result as! AWSDynamoDBPaginatedOutput 
...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("NoSQLQueryResultCell", forIndexPath: indexPath) as! NoSQLQueryResultCell
    ...
    if (!loading) && (paginatedOutput?.lastEvaluatedKey != nil) && indexPath.section == self.results!.count - 1 {
        self.loadMoreResults()
    }
    return cell

}

func loadMoreResults() {
    loading = true
    paginatedOutput?.loadNextPageWithCompletionHandler({(error: NSError?) -> Void in
        if error != nil {
            print("Failed to load more results: \(error)")
            dispatch_async(dispatch_get_main_queue(), {
                self.showAlertWithTitle("Error", message: "Failed to load more more results: \(error?.localizedDescription)")
            })
        }
        else {
            dispatch_async(dispatch_get_main_queue(), {
                self.results!.appendContentsOf(self.paginatedOutput!.items)
                self.tableView.reloadData()
                self.loading = false
            })
        }
    })
}

You can download MobileHub Sample code:

enter image description here

This map help:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.