我无法从 firebase 观察当前数据,然后将该数据加 1 并将其写回 firebase;但是我目前的代码似乎是一个永无止境的循环
func likevideoFirebase(row : Int){
let videoKey = instanceOfUser.myVideos[instanceOfUser.indexNumber].videoKey
let ref = Database.database().reference()
let userKey = instanceOfUser.myVideos
ref.child("Vidpost").child(videoKey).child("video").child(userKey).child("like").child("count").observe(.value) { (snapshot) in
var value = snapshot.value as? Int ?? 0
value += 1; self.instanceOfUser.myVideos[self.instanceOfUser.indexNumber].sentVideo[row].like.numberOfLike = value
// this where I set the data I received add 1 to it and use the function below to add it to firebase , but instead it ends up a being a never ending loop
self.setliketofirebase(like: value, row: row)
}
}
func setliketofirebase(like : Int ,row : Int ){
let videoKey = instanceOfUser.myVideos[instanceOfUser.indexNumber]
let ref = Database.database().reference()
guard let userKey = Auth.auth().currentUser?.uid else {return}
ref.child("Vidpost").child(videoKey).child("video").child(userKey).child("like").child("count").setValue(like)
}
最佳答案
你可以试试这个代码 -
ref.child("Vidpost").child(videoKey).child("video").child(userKey).child("like").child("count").setValue(like).updateChildValues(["count": like])
每次使用 setvalue() 时,您之前的值都会被替换。
关于swift - 如何观察然后从 firebase 数据库连续快速写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968356/