Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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
如何在iOS swift中获取使用firestore的用户post计数_Ios_Swift_Xcode_Firebase_Google Cloud Firestore - Fatal编程技术网

如何在iOS swift中获取使用firestore的用户post计数

如何在iOS swift中获取使用firestore的用户post计数,ios,swift,xcode,firebase,google-cloud-firestore,Ios,Swift,Xcode,Firebase,Google Cloud Firestore,我已将用户帖子、关注者和关注者从firebase迁移到firestore。现在我已经迁移了帖子,追随者和追随者,帖子,追随者也很重要 这里是我从firebase迁移到firestore的代码 import Foundation import FirebaseDatabase import FirebaseFirestore class FollowApi { var REF_FOLLOWERS = Database.database().reference().child("follo

我已将用户帖子、关注者和关注者从firebase迁移到firestore。现在我已经迁移了帖子,追随者和追随者,帖子,追随者也很重要

这里是我从firebase迁移到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
  let count = Int((querySnapshot?.documentID)!)
            print("followingCount::::\(String(describing: count))")
            completion(count!)
我试图从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
  let count = Int((querySnapshot?.documentID)!)
            print("followingCount::::\(String(describing: count))")
            completion(count!)
但还没有显示任何内容。我不知道我犯了什么错误

非常感谢您的帮助。如果您正在查询一个集合,那么它的快照将包含一个文档数组。您试图获取的是documentID,它与Firebase中的密钥相同

Firestore | Firebase

documentID=snapshot.key

documentData=snapshot.value

现在,回到要点,这里是你需要得到的计数

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

编辑注释:如何将REF_FOLLOWING.childuserId.observe.value和:{snapshot in let count=Intsnapshot.childrenCount completioncount}迁移到firestore

根据附加的数据库结构,您将获取与文档userId对应的以下内容


这是你要找的吗?let count=querySnapshot?.documents.count?@TheTiger是的,没错!!!谢谢你的回答。我已经上传了我的数据库截图。HODYPGK4WVHRLGVA3BRYPARONE22此id是当前用户。下面是当前的三个用户。如何计算当前用户的三个用户FOLLOWING如何将REF_FOLLOWING.childuserId.observe.value迁移到:{snapshot in let count=Intsnapshot.childrenCount completioncount}firestore@PvDev请检查答案的编辑部分。谢谢。。。它很好用。。我需要另外的帮助。你能在同一个问题上检查这个问题吗?