当点击like按钮时,它应该会增加与之相关的单元格上的点赞数量,但我遇到了一个问题,它用更新后的点赞数创建了一个全新的单元格。我认为问题来自于im加载单元的方式。我认为我还需要添加函数remove(),以便在快照监听器生效后清除旧数据,但我不太确定。
func loaddailymotivation() {
FirebaseReferece(.MotivationDAILY).addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) { // this line means if the chage that happened in the document was equal to added something
let data = diff.document.data()
print("we have\(snapshot.documents.count) documents in this array")
let dailyMotivationID = data["objectID"] as! String
let dailymotivationTitle = data["Motivation title"] as! String //calls the data thats heald inside of motivation title in firebase
let dailyMotivationScripture = data["daily motivation scripture"] as! String //calls the data thats heald inside of Motivation script in firebase
let dailyMotivationNumberOfLikes = data["Number of likes in daily motivation post"]as! Int
let MdataModel = motivationDailyModel(RealMotivationID: dailyMotivationID, RealmotivationTitle: dailymotivationTitle, RealmotivationScrip: dailyMotivationScripture, RealmotivationNumberOfLikes: dailyMotivationNumberOfLikes)
self.motivationThoughts.append(MdataModel)
}
//===== //=====
if (diff.type == .modified) {
print("Modified data: \(diff.document.data())")
let newdata = diff.document.data()
let dailyMotivationID = newdata["objectID"] as! String
let dailymotivationTitle = newdata["Motivation title"] as! String //calls the data thats heald inside of motivation title in firebase
let dailyMotivationScripture = newdata["daily motivation scripture"] as! String //calls the data thats heald inside of Motivation script in firebase
let dailyMotivationNumberOfLikes = newdata["Number of likes in daily motivation post"]as! Int
let MdataModel = motivationDailyModel(RealMotivationID: dailyMotivationID, RealmotivationTitle: dailymotivationTitle, RealmotivationScrip: dailyMotivationScripture, RealmotivationNumberOfLikes: dailyMotivationNumberOfLikes)
self.motivationThoughts.append(MdataModel)
// here you will receive if any change happens in your data add it to your array as you want
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
}}
发布于 2020-04-19 15:22:20
这两个分支的功能完全相同。
在modified分支中,您必须在motivationThoughts中获取相应的对象,更新它并将其放回原处,而不是创建一个新对象。
就像这样
if diff.type == .modified {
print("Modified data: \(diff.document.data())")
let newdata = diff.document.data()
let objectID = newdata["objectID"] as! String
guard let dailymotivationIndex = motivationThoughts.firstIndex(where: {$0.dailyMotivationID == objectID}) else { return }
var dailymotivation = self.motivationThoughts[dailymotivationIndex]
let dailyMotivationNumberOfLikes = newdata["Number of likes in daily motivation post"] as! Int
dailymotivation.RealmotivationNumberOfLikes = dailyMotivationNumberOfLikes
self.motivationThoughts[dailymotivationIndex] = dailymotivation
}我建议只重载modified分支中的行,而不是整个表视图。
https://stackoverflow.com/questions/61300780
复制相似问题