我尝试在控制台Firestore中修改或删除数据,并希望在我的应用中看到更改。我使用这段代码:
func checkForUpdates() {
db.collection("sweets").addSnapshotListener { querySnapshot, error in
// whereField("name", isEqualTo: String.self).addSnapshotListener { order(by: "name")
//whereField("timeStamp", isGreaterThan: Date()).addSnapshotListener {
guard let snapshot = querySnapshot else {return}
snapshot.documentChanges.forEach {
diff in
if (diff.type == .added) {
self.sweetArray.append(Sweet(dictionary: diff.document.data())!)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
if (diff.type == .modified) {
print("document modified")
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
if (diff.type == .removed) {
print("document removed")
self.tableView.reloadData()
}
}
}
}
我需要在if (diff.type == .modified)
和if (diff.type == .removed)
撰写哪些内容?
如果我在控制台Firestore中修改或删除数据,我会在控制台xCode print("document modified")
或print("document removed")
中看到。