gpt4 book ai didi

swift - 查询统计关注者数量 swift 4

转载 作者:行者123 更新时间:2023-11-30 11:05:32 25 4
gpt4 key购买 nike

我正在尝试从我的 Firebase 数据库中计算用户拥有的关注者数量,但没有成功。关注者数量返回任意值。当用户关注另一个用户时,它会在 firebase 中创建一个节点,其 userID 和 Int 值为 1,如下所示。

 fileprivate func countTrusting() {

guard let userId = self.user?.uid else { return }

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

Database.database().reference().child("following").child(userId).queryOrderedByValue().queryEqual(toValue: 1).observe(DataEventType.value, with: { (snapshot) in

let count = String(snapshot.childrenCount)
self.trustedLabel.text = "\(count) \nFollowing"

}) { (error) in
print("There was an error getting the following count:", error)
}
}

我的 Firebase 数据库

enter image description here

最佳答案

如果我理解这个问题,您想要统计用户的关注者数量。在这种情况下,不需要查询,因为唯一存在的节点是用户关注者。

此代码读取子关注者节点并打印计数。

let userFollowingRef = fbRootRef.child("following").child("some_user_uid")
userFollowingRef.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
})

关于swift - 查询统计关注者数量 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52778469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com