Reputation: 73
So far I am able to add a snapshotlistener to the collection:
db.collection("products/country/class/grade1/test").order(by: "qId").addSnapshotListener { [self] (querySnapshot, error) in
//Handle Error:
if let error = error {
print("Error getting documents: \(error.localizedDescription)")
} else {
//No Documents Found:
guard let documents = querySnapshot?.documents else {
return
}
documents.compactMap { doc in
let value = doc.data()
print (value)
}
}
}
However, I would like it where a little badge appears showing that there were databases changes & when the user presses the update button it loads only the changed (added or updated) documents
Upvotes: 0
Views: 113
Reputation: 1203
class ChannelsViewController: UITableViewController {
private var channelReference: CollectionReference {
return database.collection("channels")
}
private var channels: [Channel] = []
override func viewDidLoad() {
super.viewDidLoad()
channelListener = channelReference.addSnapshotListener { [weak self]
querySnapshot, error in
guard let self = self else { return }
guard let snapshot = querySnapshot else {
print("Error listening for channel updates: \.
(error?.localizedDescription ?? "No error")")
return
}
snapshot.documentChanges.forEach { change in
self.handleDocumentChange(change)
}
}
}
private func addChannelToTable(_ channel: Channel) {
if channels.contains(channel) {
return
}
channels.append(channel)
channels.sort()
guard let index = channels.firstIndex(of: channel) else {
return
}
tableView.insertRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
private func updateChannelInTable(_ channel: Channel) {
guard let index = channels.firstIndex(of: channel) else {
return
}
channels[index] = channel
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
private func removeChannelFromTable(_ channel: Channel) {
guard let index = channels.firstIndex(of: channel) else {
return
}
channels.remove(at: index)
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
private func handleDocumentChange(_ change: DocumentChange) {
guard let channel = Channel(document: change.document) else {
return
}
switch change.type {
case .added:
addChannelToTable(channel)
case .modified:
updateChannelInTable(channel)
case .removed:
removeChannelFromTable(channel)
}
}
}
this is an example automatically update the tableView when add,update and delete on collection name "channel" and the Channel as:
import FirebaseFirestore
struct Channel {
let id: String?
let name: String
init(name: String) {
id = nil
self.name = name
}
init?(document: QueryDocumentSnapshot) {
let data = document.data()
guard let name = data["name"] as? String else {
return nil
}
id = document.documentID
self.name = name
}
}
// MARK: - DatabaseRepresentation
extension Channel: DatabaseRepresentation {
var representation: [String: Any] {
var rep = ["name": name]
if let id = id {
rep["id"] = id
}
return rep
}
}
// MARK: - Comparable
extension Channel: Comparable {
static func == (lhs: Channel, rhs: Channel) -> Bool {
return lhs.id == rhs.id
}
static func < (lhs: Channel, rhs: Channel) -> Bool {
return lhs.name < rhs.name
}
}
Upvotes: 1