Read application data
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 in3 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 return10 }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.
1func listTodos() {2 let todo = Todo.keys3 let predicate = todo.name == "my first todo" && todo.description == "todo description"4 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in5 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
:
1import SwiftUI2import Amplify3import 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.keys6 let predicate = todo.name == "my first todo" && todo.description == "todo description"7 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in8 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 = todos14 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 in27 switch result {28 case .success(let todos):29 self.todos.append(contentsOf: todos)30 self.currentPage = todos31 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.
- Update the above method
listFirstPage()
tolistAllPages()
- Call
listNextPageRecursively()
in the success block of the query inlistAllPages()
- Update the
listNextPage()
tolistNextPageRecursively()
- Call
listNextPageRecursively()
in the success block of the query inlistNextPageRecursively()
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.keys6 let predicate = todo.name == "my first todo" && todo.description == "todo description"7 Amplify.API.query(request: .paginatedList(Todo.self, where: predicate, limit: 1000)) { event in8 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 = todos14 self.todos.append(contentsOf: todos)15 self.listNextPageRecursively() // 2. Added16 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 in28 switch result {29 case .success(let todos):30 self.todos.append(contentsOf: todos)31 self.currentPage = todos32 self.listNextPageRecursively() // 4. Added33 case .failure(let coreError):34 print("Failed to get next page \(coreError)")35 }36 }37 }38}