Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Read application data

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Query by Id

Now that you were able to make a mutation, take the Id that was printed out and use it in your query to retrieve data.

func getTodo() {
Amplify.API.query(request: .get(Todo.self, byId: "9FCF5DD5-1D65-4A82-BE76-42CB438607A0")) { event in
switch event {
case .success(let result):
switch result {
case .success(let todo):
guard let todo = todo else {
print("Could not find todo")
return
}
print("Successfully retrieved todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
}
func getTodo() -> AnyCancellable {
Amplify.API
.query(request: .get(Todo.self, byId: "9FCF5DD5-1D65-4A82-BE76-42CB438607A0"))
.resultPublisher
.sink {
if case let .failure(error) = $0 {
print("Got failed event with error \(error)")
}
}
receiveValue: { result in
switch result {
case .success(let todo):
guard let todo = todo else {
print("Could not find todo")
return
}
print("Successfully retrieved todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
}

List Query

You can get the list of items using .paginatedList with optional parameters limit and where to specify the page size and condition. By default, the page size is 1000.

func listTodos() {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
switch event {
case .success(let result):
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
}
func listTodos() -> AnyCancellable {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
let sink = Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000))
.resultPublisher
.sink {
if case let .failure(error) = $0 {
print("Got failed event with error \(error)")
}
}
receiveValue: { result in
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
return sink
}

List subsequent pages of items

If you are using SwiftUI and have SwiftUI imported in the same code file, you will need to import the class Amplify.List to resolve name collision with SwiftUI.List:

import SwiftUI
import Amplify
import class Amplify.List

For large data sets, you'll need to paginate through the results. After receiving the first page of results, you can check if there is a subsequent page and obtain the next page.

var todos: [Todo] = []
var currentPage: List<Todo>?
func listFirstPage() {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
switch event {
case .success(let result):
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
self.currentPage = todos
self.todos.append(contentsOf: todos)
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
}
func listNextPage() {
if let current = currentPage, current.hasNextPage() {
current.getNextPage { result in
switch result {
case .success(let todos):
self.todos.append(contentsOf: todos)
self.currentPage = todos
case .failure(let coreError):
print("Failed to get next page \(coreError)")
}
}
}
}

List all pages

If you want to get all pages, retrieve the subsequent page when you have successfully retrieved the first or next page.

  1. Update the above method listFirstPage() to listAllPages()
  2. Call listNextPageRecursively() in the success block of the query in listAllPages()
  3. Update the listNextPage() to listNextPageRecursively()
  4. Call listNextPageRecursively() in the success block of the query in listNextPageRecursively()

The completed changes should look like this

var todos: [Todo] = []
var currentPage: List<Todo>?
func listAllPages() { // 1. Updated from `listFirstPage()`
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
switch event {
case .success(let result):
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
self.currentPage = todos
self.todos.append(contentsOf: todos)
self.listNextPageRecursively() // 2. Added
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
}
func listNextPageRecursively() { // 3. Updated from `listNextPage()`
if let current = currentPage, current.hasNextPage() {
current.getNextPage { result in
switch result {
case .success(let todos):
self.todos.append(contentsOf: todos)
self.currentPage = todos
self.listNextPageRecursively() // 4. Added
case .failure(let coreError):
print("Failed to get next page \(coreError)")
}
}
}
}