swift - 如何观察然后从 firebase 数据库连续快速写入

标签 swift loops firebase firebase-realtime-database observers

我无法从 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/

相关文章:

javascript - 在 JAVASCRIPT 中引用循环中的值

android - 当并发连接数超过 Firebase 中的限制时额外连接会发生什么

ios - 如何更改 WidgetKit 中心 ‘Add Widget’ 按钮的颜色

Swift UITableView 获取选定的表项

c# - 如何为每个循环创建新的 div

c# - C#计时器在循环期间抛出错误?

ios - 无法将类型 'UserFile' 的值转换为预期参数 'User'

c++ - 我们可以使用 FireBase 云消息传递在 Windows 桌面/控制台或 Linux 控制台应用程序上使用 C++ 发送或接收消息或两种方式吗?

Xcode/Swift - 多个 View Controller 中的元素

ios - 如何根据内容创建具有动态单元格高度的 UITableViewCell