Page updated Jan 16, 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.

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

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.

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

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:

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.

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

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}