DEV Community

loading...

Simple Todo - Part 3 of 6

Related Code
Mobile Application Development. Scalable Architecture Design. Custom Backend Development. Application Security. Performance Engineering. Analytics. Maintenance.
・2 min read

As a follow-up to our previous implementation, we will add some basic server functionalities to our existing demo app.

Before doing anything else, we need to set up the GQLServer by using the following (we will use one common server instance for all server actions).

let key = "da2-6tcxkcwclrbdjcqmkln7pjjg2y"
let link = "https://7nmlvmururcwrhebjw76irplwa.appsync-api.us-east-2.amazonaws.com/graphql"
let gqlserver = GQLServer(AppSync: link, key: key)
Enter fullscreen mode Exit fullscreen mode

Let's save the necessary Mutations and Queries into an appsync.graphql file, so we can simply use them in the codebase just referring to their names.

Screenshot

After the steps above, creating and updating Todo items (on the server) can be done by using the following methods.

func createTodo(_ todo: Todo) {

  let query = GQLQuery["CreateTodo"]
  let variables = ["object": todo.values()]

  gqlserver.mutation(query, variables) { result, error in
    if let error = error {
      print(error.localizedDescription)
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
func updateTodo(_ todo: Todo) {

  let query = GQLQuery["UpdateTodo"]
  let variables = ["object": todo.values()]

  gqlserver.mutation(query, variables) { result, error in
    if let error = error {
      print(error.localizedDescription)
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Fetching Todo items from the server might seem a little trickier at first, but it's also not rocket science.

func fetchTodos() {

  let query = GQLQuery["ListTodos"]
  let updatedAt = LastUpdated["Todo"]
  let variables = ["updatedAt": updatedAt]

  gqlserver.query(query, variables) { result, error in
    if let error = error {
      print(error.localizedDescription)
    } else {
      if let dictionary = result.values.first as? [String: Any] {
        if let array = dictionary["items"] as? [[String: Any]] {
          for values in array {
            self.updateDatabase(values)
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
func updateDatabase(_ values: [String: Any]) {

  gqldb.updateInsert("Todo", values)
  LastUpdated.update("Todo", values)
}
Enter fullscreen mode Exit fullscreen mode

We need the LastUpdated implementation to identify what is the latest fetched object we have, so we can avoid fetching todo items multiple times from the server.

In the TodoView.swift file we need to add few extra lines of code.

func actionFetch() {

  ServerData.fetchTodos()
}
Enter fullscreen mode Exit fullscreen mode
func createTodo(_ title: String) {

  let todo = Todo()
  todo.title = title
  todo.insert(gqldb)

  ServerData.createTodo(todo)
}
Enter fullscreen mode Exit fullscreen mode
func completeTodo(_ todo: Todo) {

  todo.completed = true
  todo.updatedAt = Date()
  todo.update(gqldb)

  ServerData.updateTodo(todo)
}
Enter fullscreen mode Exit fullscreen mode

And pretty much that's all we need for exchanging data between the local and remote databases. You can check the full source code here.

Discussion (0)