Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 4和Firebase如何计算子值_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Swift 4和Firebase如何计算子值

Swift 4和Firebase如何计算子值,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我试图通过返回值等于1的所有用户来计算每个人的点击次数。这是正确的做法吗。下面是我用来设置它们是否运行的函数。有人能帮我把每个人的人数还给我吗 func didTapGoing(for cell: HomePostCell) { guard let indexPath = collectionView?.indexPath(for: cell) else { return } var post = self.posts[indexPath.item] guard let

我试图通过返回值等于1的所有用户来计算每个人的点击次数。这是正确的做法吗。下面是我用来设置它们是否运行的函数。有人能帮我把每个人的人数还给我吗

func didTapGoing(for cell: HomePostCell) {
    guard let indexPath = collectionView?.indexPath(for: cell) else { return }
    var post = self.posts[indexPath.item]

    guard let postId = post.id else { return }

    guard let uid = FIRAuth.auth()?.currentUser?.uid else { return }

    let values = [uid: post.isGoing == true ? 0 : 1]
    FIRDatabase.database().reference().child("going").child(postId).updateChildValues(values) { (err, _) in

        if let err = err {
            print("Failed to pick going", err)
            return
        }

        post.isGoing = !post.isGoing
        self.posts[indexPath.item] = post
        self.collectionView?.reloadItems(at: [indexPath])
    }
}

您共享的代码不读取任何数据,只使用
updatechildvalue
更新数据

要计算子节点的数量,需要读取这些节点,然后调用

如果只想计算值为1的子节点,则应执行以下操作:

FIRDatabase.database().reference().child("going").child(postId)
  .queryOrderedByValue().queryEqual(toValue: 1)
  .observe(DataEventType.value, with: { (snapshot) in
    print(snapshot.childrenCount)
  })

有关这方面的更多信息,请阅读上的Firebase文档。

您共享的代码不读取任何数据,只使用
updateChildValue更新它

要计算子节点的数量,需要读取这些节点,然后调用

如果只想计算值为1的子节点,则应执行以下操作:

FIRDatabase.database().reference().child("going").child(postId)
  .queryOrderedByValue().queryEqual(toValue: 1)
  .observe(DataEventType.value, with: { (snapshot) in
    print(snapshot.childrenCount)
  })

有关这方面的更多信息,请阅读上的Firebase文档。

我知道我不确定如何操作。所以我展示了我是如何按“前进”按钮的。是否可以计算数字1的值?为此添加了一个示例。我知道我不确定如何计算。所以我展示了我是如何按“前进”按钮的。是否可以计算数字1的值?为此添加了一个示例。