12.8. Observing Firestore Updates: Updating the TableView
//
// ViewController.swift
// App12
//
// Created by Sakib Miazi on 6/1/23.
//
import UIKit
import FirebaseAuth
import FirebaseFirestore
class ViewController: UIViewController {
//code omitted...
let database = Firestore.firestore()
override func loadView() {
view = mainScreen
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//MARK: handling if the Authentication state is changed (sign in, sign out, register)...
handleAuth = Auth.auth().addStateDidChangeListener{ auth, user in
if user == nil{
//code omitted...
}else{
//code omitted...
//MARK: Observe Firestore database to display the contacts list...
self.database.collection("users")
.document((self.currentUser?.email)!)
.collection("contacts")
.addSnapshotListener(includeMetadataChanges: false, listener: {querySnapshot, error in
if let documents = querySnapshot?.documents{
self.contactsList.removeAll()
for document in documents{
do{
let contact = try document.data(as: Contact.self)
self.contactsList.append(contact)
}catch{
print(error)
}
}
self.contactsList.sort(by: {$0.name < $1.name})
self.mainScreen.tableViewContacts.reloadData()
}
})
}
}
}
//code omitted...
}

YAY!!! We built our first app using Firebase!!!
Last updated