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 deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Swift to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

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))