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.
Please refer to it as it is summarized here. [IOS] How to get table name from AWS DynamoDB
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.
Podfile
target 'aws-sdk' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'AWSDynamoDB'
end
Install pod and reopen Xcode.
Terminal
$ pod install --repo-update
$ xed .
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 | ○ |
String | ||
password | String |
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 protected] | |
password | password |
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. .. ..
User.swift
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"
}
}
This time, I'm using scan because I want to get all the data.
ViewController.swift
import UIKit
import AWSDynamoDB
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
fetchDynamoDBData()
}
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)")
return
}
print(items)
}
}
}
Now when you build, the data will be displayed in the console.
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.
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.
ViewController.swift
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")
return
}
if key != nil {
// MEMO:Update the page to start
scanExpression.exclusiveStartKey = key
fetchDynamoDBData()
}
}
}
For your reference. https://github.com/shungo0525/aws-sdk
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