Sign up for Hasura Newsletter
Loading...

Fetch public todos - subscription

Let's define the graphql query to be used:

Open Todo/FeedVC.swift and add the following variable and initialize in viewWillAppear and viewWillDisappear.

githubTodo/FeedVC.swift
+ var apollo: ApolloClient!
+ var newPublicTodos: Cancellable?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if( SessionManager.shared.credentials?.accessToken! != nil ) {
self.apollo = NetworkManager.shared.apolloClient
}
Utils.shared.setLeftPaddingInput(ofInput: todoInput)
- toggleNotif()
+ subscribeNewPublicTodos()
setupUI(loading: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
+ unSubscribeNewPublicTodos()
}

Now let's define the subscription to get notified about new public todos

// Subscribe to New Public Todos
private func subscribeNewPublicTodos() {
newPublicTodos = apollo.subscribe(subscription: NotifyNewPublicTodosSubscription()) { (result, error) in
if((error) != nil) { dump(error); return }
guard let data = result?.data else { return }
}
}

Also lets add a function to fetch the initial public todos.

// Get Public Todos from cloud
private func getInitialPublicTodos(){
apollo.fetch(query: GetInitialPublicTodosQuery()) { (result, err) in
guard let data = result?.data else { return }
let newTodos = data.todos.compactMap { PublicTodo(title: $0.title, username: $0.user.name, id: $0.id) }
self.publicTodos = newTodos
DispatchQueue.main.async {
self.todoPublicTable.reloadData()
self.setupUI(loading: false)
}
}
}

and add that in viewWillAppear

super.viewWillAppear(animated)
if( SessionManager.shared.credentials?.accessToken! != nil ) {
self.apollo = NetworkManager.shared.apolloClient
+ if self.publicTodos.isEmpty {
+ getInitialPublicTodos()
+ }
}

What does the Subscription do?

The query fetches todos with a simple condition; is_public must be true. We also limit the number of todos to 1, since we would just like to get notified whenever a new todo comes in. We sort the todos by its latest createdAt time according to the schema. We specify which fields we need for the todos node.

Right now we don't return anything when new data comes in. getInitialPublicTodos will map the result.data to the publicTodos so, we can view them on the tableView.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin