Getting Started with AWS AppSync for iOS

Learn how to consume GraphQL APIs in your SwiftUI iOS apps in a simple and type-safe way using AWS AppSync framework. By Alex Brown.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Deleting To Dos

The final thing you need add is a way to delete rows. Swipe-to-delete already exists in the UI, so you just need to wire it up.

Open TodoListViewModel.swift, and you’ll notice three delete methods at the top. These will act as helpers to delete to do items from their respective list.

Add the following method:

func delete(todo: Todo) {
  Amplify.DataStore.delete(todo) { result in
    switch result {
    case .success:
      print("Deleted item: \(todo.name)")
    case .failure(let error):
      print("Could not update data with error: \(error)")
    }
  }
}

This method deletes the model from the data store by calling delete(_:) from the Amplify framework.

Next, replace the three delete methods above with the following:

// 1
func deleteItems(at offsets: IndexSet, from todoList: inout [Todo]) {
  for index in offsets {
    let todo = todoList[index]
    delete(todo: todo)
  }

  todoList.remove(atOffsets: offsets)
}

// 2
func deleteTodos(at offsets: IndexSet) {
  deleteItems(at: offsets, from: &todos)
}

// 3
func deleteCompletedTodos(at offsets: IndexSet) {
  deleteItems(at: offsets, from: &completedTodos)
}

Here’s what you’ve done:

  1. The first delete method calls delete(at:from:), which you just added.
  2. This method routes the call to delete with the todos array.
  3. This method routes the call to delete with the completedTodos array.

Build and run the project. You can now swipe to delete todos!

Deleting a todo gif

You now have a to do list that allows you to add, edit and delete to dos. It works offline and stays synchronized to an AWS back end.

Where to Go From Here?

You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.

You now know the basics of integrating and using AppSync with Amplify in your iOS apps — but there’s a lot more to learn! GraphQL can do much more than what you’ve covered here.

When you’re ready for your next steps with AWS and Amplify, check out Using AWS as a Back End: Authentication & API and Using AWS as a Back End: The Data Store API.

Check out our tutorial GraphQL Using the Apollo Framework: Getting Started to see more practical examples. You can also learn a lot more about GraphQL on the official GraphQL website.

You should also look at the official AWS AppSync tutorials Amazon has produced for further learning.

If you have any questions, please join the discussion in the forum below!