简体   繁体   English

Swift Firestore 使用数组获取文档字段

[英]Swift Firestore using an Array to get document fields

I am using an array named PosterEmail to search for a document field name "First Name" in my Firestore Database.我正在使用名为 PosterEmail 的数组在我的 Firestore 数据库中搜索文档字段名称“First Name”。

Here you can see my database set up.在这里你可以看到我的数据库设置。

FirebaseSetup Firebase 设置

enter image description here在此处输入图像描述

As you can see my Database is Public then in the document I have the user's email as the document name and in that document I have their information such as their first name, last name, and profile photo url.如您所见,我的数据库是公共的,然后在文档中我有用户的 email 作为文档名称,在该文档中我有他们的信息,例如他们的名字、姓氏和个人资料照片 url。

I search for the document id using an Array我使用数组搜索文档 ID

The PosterEmail array is PosterEmail 数组是

PosterEmail = ["chainsawloco@yahoo.com", "allmight@gmail.com", "allmight@gmail.com", "chainsawloco@yahoo.com"] PosterEmail = [“chainsawloco@yahoo.com”、“allmight@gmail.com”、“allmight@gmail.com”、“chainsawloco@yahoo.com”]

I am going through my PosterEmail index by setting a variable "profilecount" to 0 and adding 1 to it everytime to go through the PosterArray我通过将变量“profilecount”设置为 0 并每次通过 PosterArray 将 go 加 1 来检查我的 PosterEmail 索引

let docRef = db.collection("Public").document("\(self.PosterEmail[self.profilecount])")

But it seems that the code above never searches for the Firebase document named after the second item in my Array但似乎上面的代码从不搜索以我的数组中第二项命名的 Firebase 文档

The result is just结果只是

["Irving ", "Irving ", "Irving ", "Irving "] [“欧文”、“欧文”、“欧文”、“欧文”]

The result should end up as [Irving, Allmight, Allmight, Irving]结果应该以 [Irving, Allmight, Allmight, Irving] 结尾

Is there something I"m doing wrong?有什么我做错了吗?

Below is where I call my getPosteInformation() in another method called getDatFromFirestore() (ehhh I know my method name has a typo but I can fix that later)下面是我在另一个名为 getDatFromFirestore() 的方法中调用我的 getPosteInformation() 的地方(嗯,我知道我的方法名称有错字,但我可以稍后修复)

               if let postedBy = document.get("postedBy") as? String {
                    print("Postby = document.get(postedby = \(postedBy)")
                    self.PosterEmail.append(postedBy)

                    if self.PosterEmail.count > 0 {
                               self.getPosteInformation()
                          }
                }
            }
        }
    }

Below you can see my full code.下面你可以看到我的完整代码。

func getDatFromFirestore() {

    let firestoreDatabase = Firestore.firestore()

    firestoreDatabase.collection("Posts").order(by: "Date" , descending : true).getDocuments { (snapshot, error) in
        if error != nil {
            print(error?.localizedDescription ?? "Connection Error")
        } else {
            self.userPostImageArray.removeAll(keepingCapacity: false)
            self.userCommentArray.removeAll(keepingCapacity: false)
            self.userCommentArray.removeAll(keepingCapacity: false)
            self.likeArray.removeAll(keepingCapacity: false)
            self.PosterEmail.removeAll(keepingCapacity: false)
            self.userProfilePhotoArray.removeAll(keepingCapacity: false)
            self.PosterFirstNameArray.removeAll(keepingCapacity: false)
            self.PosterLastNameArray.removeAll(keepingCapacity: false)

            for document in snapshot!.documents {

                let documentID = document.documentID

                self.documentIDArray.append(documentID)

                if let postDescription = document.get("PostDescription") as? String {
                    self.userPostDescription.append(postDescription)
                }

                if let imageUrl = document.get("imageUrl") as? String {
                    self.userPostImageArray.append(imageUrl)
                }

                if let PostLikes = document.get("Likes") as? Int {
                    self.likeArray.append(PostLikes)
                }

                if let postTimeStamp = document.get("Date") as? Timestamp {

                    let date = postTimeStamp.dateValue()

                    let formatter = DateFormatter()
                    formatter.dateFormat = "HH:mm MM/dd/yyyy"
                    let dateString = formatter.string(from: date)
                    let timeStampAsString = dateString

                    self.postDate.append(timeStampAsString)
                }

                if let postedBy = document.get("postedBy") as? String {
                    print("Postby = document.get(postedby = \(postedBy)")
                    self.PosterEmail.append(postedBy)

                    if self.PosterEmail.count > 0 {
                               self.getPosteInformation()
                           }
                }
            }
        }
    }
    self.VidaFeed.reloadData()
}

func getPosteInformation() {

    print("")
    print(PosterEmail[profilecount])
    print(profilecount)
    print("")


    print(PosterEmail)
    let docRef = db.collection("Public").document("\(self.PosterEmail[self.profilecount])")

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

            if self.profilecount < self.PosterEmail.count {

                if let PosterFirstName = document.get("First Name") as? String {
                    self.PosterFirstNameArray.append(PosterFirstName)
                    print(self.PosterFirstNameArray)

                    print("\(self.profilecount)")
                    if self.PosterEmail.count > self.profilecount {
                        self.profilecount = self.profilecount + 1
                    }

                }
            }
        } else {
            print("Document does not exist")
        }
    }
}
           if let postedBy = document.get("postedBy") as? String {
                print("Postby = document.get(postedby = \(postedBy)")
                self.PosterEmail.append(postedBy)
                self.PosterFirstNameArray.append("")

                if self.PosterEmail.count > 0 {
                   self.getPosteInformation(profCount: self.profileCount)
                }

                if self.PosterEmail.count > self.profilecount {
                    self.profilecount = self.profilecount + 1
                }
            }

And now if you could modify this method like this:现在,如果您可以像这样修改此方法:

            func getPosteInformation(profCount:Int) {

                //and inside the async call back instead of the following try 
                /*if let PosterFirstName = document.get("First Name") as? String {
                self.PosterFirstNameArray.append(PosterFirstName)
                print(self.PosterFirstNameArray)

                print("\(self.profilecount)")
                if self.PosterEmail.count > self.profilecount {
                    self.profilecount = self.profilecount + 1
                }

                }*/

                if let PosterFirstName = document.get("First Name") as? String              {
                self.PosterFirstNameArray[profCount] = PosterFirstName
                print(self.PosterFirstNameArray)

                print("\(profCount)")


            }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM