[SWIFT] [IOS] How to get data from DynamoDB


I think that I may develop an application that works with DynamoDB with swift, but since there were few articles, I tried to summarize the method of acquiring data.

Development environment

1. AWS authentication settings

Please refer to it as it is summarized here. [IOS] How to get table name from AWS DynamoDB

2. Add AWS DyanamoDB to pod

It is assumed that the pod is already installed in the project. If it is not installed, please install it.

Add AWD DyanmoDB to your Podfile.


target 'aws-sdk' do
  # Comment the next line if you don't want to use dynamic frameworks

  pod 'AWSDynamoDB'

Install pod and reopen Xcode.


$ pod install --repo-update
$ xed .

3. Create table and insert data in AWS DynamoDB

Press Create Table to create the table. This time, I changed the table name to user.

User table

Column name Mold Primary partition key
id Number
email String
password String

スクリーンショット 2020-12-31 15.27.06.png

Once the table is created, select the tab for items. Then press Create Item to create the data. I created the following data.

Column name value
email [email protected]
password password

スクリーンショット 2020-12-31 15.27.45.png

4. Create model

Create a model to get DynamoDB data. Please change to the class name and property as appropriate.

Data cannot be obtained without @objc. I'm addicted to this, so be careful not to forget it. .. ..


import AWSDynamoDB

class User: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
    @objc private var id: NSNumber = 0
    @objc private var email: String = ""
    @objc private var password: String = ""

    class func dynamoDBTableName() -> String {
        return "user"  //Describe the table name created in DyanmoDB

    class func hashKeyAttribute() -> String {
        return "id"

5. Create a method to get the data

This time, I'm using scan because I want to get all the data.


import UIKit
import AWSDynamoDB

class ViewController: UIViewController {

    override func viewDidLoad() {

    private func fetchDynamoDBData() {
        let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
        let scanExpression = AWSDynamoDBScanExpression()

        //User part is the class name of the created Model
        dynamoDBObjectMapper.scan(User.self, expression: scanExpression).continueWith() { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Void in
            guard let items = task.result?.items as? [User] else { return }
            if let error = task.error as NSError? {
                print("The request failed. Error: \(error)")

Now when you build, the data will be displayed in the console.

スクリーンショット 2020-12-31 18.03.21.png

If DyanmoDB has a lot of data

If there is a lot of data, you may not be able to get all the data at once even if you scan. (I didn't notice it and was really into it ...) You can only get up to 1MB with a single scan.

(Using Scan with DynamoDB)

If not all items have been acquired, a task.result? .LastEvaluatedKey will appear. In other words, unless this becomes nil, you will not be able to get all the records. (Similar to the current page of pagenation)

If you have a lot of data but want to get all of them, you need to update the pagenation. As shown below, if task.result? .LastEvaluatedKey exists, update the starting page to task.result? .LastEvaluatedKey and fetch it again.


func fetchDynamoDBData() {
    dynamoDBObjectMapper.scan(User.self, expression: scanExpression).continueWith() { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Void in

        guard let key = task.result?.lastEvaluatedKey else {
            print("end of scan")

        if key != nil {
            // MEMO:Update the page to start
            scanExpression.exclusiveStartKey = key


For your reference. https://github.com/shungo0525/aws-sdk

Reference article

https://stackoverflow.com/questions/26958637/best-way-to-make-amazon-aws-dynamodb-queries-using-swift https://stackoverflow.com/questions/34049054/aws-dynamodb-swift-query-with-multiple-hashkeyvalues

Recommended Posts

[IOS] How to get data from DynamoDB
[IOS] How to get the table name from AWS DynamoDB
[IOS14] How to get Data type image data directly from PHPickerViewController?
How to get and add data from Firebase Firestore in Ruby
How to get a heapdump from a Docker container
How to get Class from Element in Java
How to get SIMD optimization from HotSpot JavaVM
[iOS] How to read Carthage
How to migrate from JUnit4 to JUnit5
How to get jdk etc from oracle with cli
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to get the longest information from Twitter as of 12/12/2016
How to use Java HttpClient (Get)
How to get started with slim
How to use Spring Data JDBC
[How to install Spring Data Jpa]
How to get parameters in Spark
Get GTFS data from OpenTripPlanner Graph
How to get along with Rails
How to change from HTML to Haml
How to delete only specific data from data created using Form object
[Swift UI] How to get the startup status of the application [iOS]
[Java] How to convert from String to Path type and get the path
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
How to adjustTextPosition with iOS Keyboard Extension
[Rails] How to convert from erb to haml
[Java] How to add data to List (add, addAll)
[Note] How to get started with Rspec
How to call Swift 5.3 code from Objective-C
[Java] How to get the current directory
[Flutter] How to use C / C ++ from Dart?
Java: How to send values from Servlet to Servlet
How to get the date in java
How to delete data with foreign key
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to overwrite Firebase data in Swift
[Rails] How to handle data using enum
Try to get data from database using MyBatis in Micronaut + Kotlin project
How to get the setting value (property value) from the database in Spring Framework
[iOS] How to make UI parts added from code fit in Safe Area
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to get keycloak credentials in interceptor class
How to link Rails6 Vue (from environment construction)
[Java] How to get and output standard input
How to dump from database (DB) to seeds file
How to deploy
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to get today's day of the week
How to get started with Eclipse Micro Profile
[Java] How to get random numbers excluding specific numbers
How to get an arbitrary digit from a number of 2 or more digits! !!
[Java] How to switch from open jdk to oracle jdk
How to get and study java SE8 Gold
I want to get only the time from Time type data ...! [Strftime] * Additional notes
How to get resource files out with spring-boot
How to create hierarchical category data using ancestry
How to get the date from the JavaScript Date type that C # developers are addicted to
[Java] How to get the redirected final URL
[Rails] How to get success and error messages
[Java] How to get the authority of the folder
[ruby] How to receive values from standard input?