如何在iOS中使用迅速对公司的FireStore获得的用户发帖量(How to get post c

2019-10-30 02:37发布

我已迁移的用户后,追随者和从火力点以下到公司的FireStore。 我现在已经迁移后,追随者和以下和后,追随者算过。

在这里,我已经从火力迁移代码公司的FireStore。

 import Foundation
import FirebaseDatabase
 import FirebaseFirestore

class FollowApi {


var REF_FOLLOWERS = Database.database().reference().child("followers")
var REF_FOLLOWING = Database.database().reference().child("following")
let db = Firestore.firestore()

func followAction(withUser id: String) {


    let docRef = db.collection("user-posts").document(id)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")

            self.db.collection("feed").document(API.User.CURRENT_USER!.uid).setData([document.documentID: true])

        } else {
            print("Document does not exist")
        }
    }



   self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: true])
   self.db.collection("following").document(API.User.CURRENT_USER!.uid).updateData([id: true])

}

func unFollowAction(withUser id: String) {

    let docRef = db.collection("user-posts").document(id)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")



 self.db.collection("feed").document(API.User.CURRENT_USER!.uid).delete()


        } else {
            print("Document does not exist")
        }
    }



    self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: NSNull()])
    self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: NSNull()])

 }

func isFollowing(userId: String, completed: @escaping (Bool) -> Void) {

    let docRef = db.collection("followers").document(userId)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {

            print("documnetData::::\(String(describing: document.data()))")
            if let dataDescription = document.data(), let _ = dataDescription[API.User.CURRENT_USER!.uid] as? Bool {

                completed(true)
            }

            completed(false)

        } else {
           completed(false)
        }
    }


}





    func fetchCountFollowing(userId: String, completion: @escaping (Int) -> Void) {
  //     REF_FOLLOWING.child(userId).observe(.value, with: {
  //                snapshot in
 //                let count = Int(snapshot.childrenCount)
 //                completion(count)
 //            })



        db.collection("following").document(userId).getDocument { (querySnapshot, error) in

            let count = Int((querySnapshot?.documentID)!)
            print("followingCount::::\(String(describing: count))")
            completion(count!)
        }

    }

 }//followAPI

我试图让下面从公司的FireStore计数。

  let count = Int((querySnapshot?.documentID)!)
            print("followingCount::::\(String(describing: count))")
            completion(count!)

但不显示任何任何尚未全部。 我不知道我做了什么错?

任何帮助非常赞赏请....

Answer 1:

如果要查询的collection ,然后它的snapshot将包含数组documents 。 什么是你想要得到的是一个documentID是一样的keyFirebase

公司的FireStore | 火力地堡

documentID = snapshot.key

documentData = snapshot.value

现在想来,主要的一点,这里是你需要得到算什么。

let count = querySnapshot?.documents.count
print(count)

EDIT对于评论: 我如何迁移REF_FOLLOWING.child(用户id).observe(.value的,具有:{在设计快照= INT(snapshot.childrenCount)完成(计数)})到公司的FireStore

基于连接的数据库结构,你获取following相应userId这是一个Document

REF_FOLLOWING.document(userId).getDocument { (snapshot, error) in
    if let _error = error {
       print(_error.localizedDescription)
       return
    }

    guard let _snapshot = snapshot else {return}

    /// This is a single document and it will give you "[String:Any]" dictionary object. So simply getting its count is the result you needed.
    let dict = _snapshot.data()
    print(dict.count)
}


文章来源: How to get post count of user using firestore in iOS swift