Page updated Jan 16, 2024

Create, update, and delete 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.

Run a mutation

Now that the client is set up, you can run a GraphQL mutation with Amplify.API.mutate to create, update, and delete your data.

Make sure you have the following imports at the top of your file

1import Amplify
2import AWSPluginsCore
1func updateTodo() {
2 // Retrieve your Todo using Amplify.API.query
3 var todo = Todo(name: "my first todo", description: "todo description")
4 todo.description = "updated description"
5 Amplify.API.mutate(request: .update(todo)) { event in
6 switch event {
7 case .success(let result):
8 switch result {
9 case .success(let todo):
10 print("Successfully created todo: \(todo)")
11 case .failure(let error):
12 print("Got failed result with \(error.errorDescription)")
13 }
14 case .failure(let error):
15 print("Got failed event with error \(error)")
16 }
17 }
18}

Make sure you have the following imports at the top of your file

1import Amplify
2import AWSPluginsCore
3import Combine
1func updateTodo() -> AnyCancellable {
2 // Retrieve your Todo using Amplify.API.query
3 var todo = Todo(name: "my first todo", description: "todo description")
4 todo.description = "updated description"
5 let sink = Amplify.API.mutate(request: .update(todo))
6 .resultPublisher
7 .sink {
8 if case let .failure(error) = $0 {
9 print("Got failed event with error \(error)")
10 }
11 }
12 receiveValue: { result in
13 switch result {
14 case .success(let todo):
15 print("Successfully created todo: \(todo)")
16 case .failure(let error):
17 print("Got failed result with \(error.errorDescription)")
18 }
19 }
20 return sink
21}

To create data, replace the request with .create

1Amplify.API.mutate(request: .create(todo))

To delete data, replace the request with .delete

1Amplify.API.mutate(request: .delete(todo))