Page updated Nov 15, 2023

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.

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)") } } }
1func getTodo() {
2 Amplify.API.query(request: .get(Todo.self, byId: "9FCF5DD5-1D65-4A82-BE76-42CB438607A0")) { event in
3 switch event {
4 case .success(let result):
5 switch result {
6 case .success(let todo):
7 guard let todo = todo else {
8 print("Could not find todo")
9 return
10 }
11 print("Successfully retrieved todo: \(todo)")
12 case .failure(let error):
13 print("Got failed result with \(error.errorDescription)")
14 }
15 case .failure(let error):
16 print("Got failed event with error \(error)")
17 }
18 }
19}

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)") } } }
1func listTodos() {
2 let todo = Todo.keys
3 let predicate = todo.name == "my first todo" && todo.description == "todo description"
4 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
5 switch event {
6 case .success(let result):
7 switch result {
8 case .success(let todos):
9 print("Successfully retrieved list of todos: \(todos)")
10 case .failure(let error):
11 print("Got failed result with \(error.errorDescription)")
12 }
13 case .failure(let error):
14 print("Got failed event with error \(error)")
15 }
16 }
17}

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
1import SwiftUI
2import Amplify
3import 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)") } } } }
1var todos: [Todo] = []
2var currentPage: List<Todo>?
3
4func listFirstPage() {
5 let todo = Todo.keys
6 let predicate = todo.name == "my first todo" && todo.description == "todo description"
7 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
8 switch event {
9 case .success(let result):
10 switch result {
11 case .success(let todos):
12 print("Successfully retrieved list of todos: \(todos)")
13 self.currentPage = todos
14 self.todos.append(contentsOf: todos)
15 case .failure(let error):
16 print("Got failed result with \(error.errorDescription)")
17 }
18 case .failure(let error):
19 print("Got failed event with error \(error)")
20 }
21 }
22}
23
24func listNextPage() {
25 if let current = currentPage, current.hasNextPage() {
26 current.getNextPage { result in
27 switch result {
28 case .success(let todos):
29 self.todos.append(contentsOf: todos)
30 self.currentPage = todos
31 case .failure(let coreError):
32 print("Failed to get next page \(coreError)")
33 }
34 }
35 }
36}

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)") } } } }
1var todos: [Todo] = []
2var currentPage: List<Todo>?
3
4func listAllPages() { // 1. Updated from `listFirstPage()`
5 let todo = Todo.keys
6 let predicate = todo.name == "my first todo" && todo.description == "todo description"
7 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in
8 switch event {
9 case .success(let result):
10 switch result {
11 case .success(let todos):
12 print("Successfully retrieved list of todos: \(todos)")
13 self.currentPage = todos
14 self.todos.append(contentsOf: todos)
15 self.listNextPageRecursively() // 2. Added
16 case .failure(let error):
17 print("Got failed result with \(error.errorDescription)")
18 }
19 case .failure(let error):
20 print("Got failed event with error \(error)")
21 }
22 }
23}
24
25func listNextPageRecursively() { // 3. Updated from `listNextPage()`
26 if let current = currentPage, current.hasNextPage() {
27 current.getNextPage { result in
28 switch result {
29 case .success(let todos):
30 self.todos.append(contentsOf: todos)
31 self.currentPage = todos
32 self.listNextPageRecursively() // 4. Added
33 case .failure(let coreError):
34 print("Failed to get next page \(coreError)")
35 }
36 }
37 }
38}