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

Page updated Apr 29, 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

import Amplify
import AWSPluginsCore
func updateTodo() {
// Retrieve your Todo using Amplify.API.query
var todo = Todo(name: "my first todo", description: "todo description")
todo.description = "updated description"
Amplify.API.mutate(request: .update(todo)) { event in
switch event {
case .success(let result):
switch result {
case .success(let todo):
print("Successfully created todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
}

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

import Amplify
import AWSPluginsCore
import Combine
func updateTodo() -> AnyCancellable {
// Retrieve your Todo using Amplify.API.query
var todo = Todo(name: "my first todo", description: "todo description")
todo.description = "updated description"
let sink = Amplify.API.mutate(request: .update(todo))
.resultPublisher
.sink {
if case let .failure(error) = $0 {
print("Got failed event with error \(error)")
}
}
receiveValue: { result in
switch result {
case .success(let todo):
print("Successfully created todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
return sink
}

To create data, replace the request with .create

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

To delete data, replace the request with .delete

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