簡體   English   中英

查詢以計數人數追隨者迅速4

[英]Query to count number followers swift 4

我正在嘗試計算用戶從我的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數據庫

在此處輸入圖片說明

如果我理解這個問題,則希望統計一個用戶的關注者數量。 在那種情況下,不需要查詢,因為將存在的唯一節點是用戶關注者。

此代碼讀取子關注者節點並打印計數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM