首页 > 解决方案 > 如何使用 Swift 从 Firestore 检索数组中保存的数据

问题描述

这似乎应该是一件很简单的事情,但似乎找不到解决方案。我在 Firestore 中有一些数据保存在一个数组中,我需要获取这些数据并将其放入我的 swift 应用程序中的二维数组中。我很菜鸟所以提前道歉!

这是我试图从 Firestore 获取的数据 火库

这是我用来查询我的文档的代码,然后循环遍历结果,提取数据

fireStoreDatabase.collection("Posts").whereField("postedTo", arrayContains: userId).order(by: "date", descending: true).addSnapshotListener { (snapshot, error) in
        if error != nil {

            print(error?.localizedDescription)
        } else {
            if snapshot?.isEmpty != true && snapshot != nil {
                print("Posted data got")
                
                //clear the arrays to stop duplicates when we do an upload and pull in the data again
                self.postedShoutTextArray.removeAll(keepingCapacity: false)
                self.postedByArray.removeAll(keepingCapacity: false)
                self.postedDateArray.removeAll(keepingCapacity: false)
                self.postedQuestionsArray.removeAll(keepingCapacity: false)
                
                //loop through the firestore
                for document in snapshot!.documents {
                    //add data to the arrays
                    if let shoutText = document.get("shoutText") as? String {
                        self.postedShoutTextArray.append(shoutText)
                    }
                    if let postedBy = document.get("postedBy") as? String {
                        self.postedByArray.append(postedBy)
                    }
                    if let date = document.get("date") as? String {
                        self.postedDateArray.append(date)
                    }
                    
                    if let pollQuestions = document.get("pollQuestions") as? [String] {
                        self.postedQuestionsArray = pollQuestions
                    } else {
                        print("no array data")
                    }

                    self.receivedCollectionView.reloadData()
                }
                
            } else {
                print("GET POSTED MESSAGES no data")
            }
            
        }
    }

所以我希望数据进入一个二维数组(如果可能的话),其中包含来自 pollQuestions 数组的数据,用于我循环遍历的每个文档(这有意义吗?)。

一直在搜索,并看到对地图等的引用,但没有找到解决方案。

任何帮助表示赞赏!

标签: arraysswiftfirebasegoogle-cloud-firestore

解决方案


与其将每个属性存储在单独的数组中,不如考虑使用struct. 就像是:

struct Item {
    var shoutText: String?
    var postedBy: String?
    var date: String?
    var pollQuestions : [String]
}

然后,在您的视图控制器上,声明一个属性:

var items: [Item] = []

然后,在您的快照侦听器中,您可以填充该数组:

func getData() {
    Firestore.firestore().collection("Posts").whereField("postedTo", arrayContains: userId).order(by: "date", descending: true).addSnapshotListener { (snapshot, error) in
        if error != nil {
            print(error?.localizedDescription)
        } else {
            if let snapshot = snapshot, !snapshot.isEmpty {
                print("Posted data got")
                
                self.items = snapshot.documents.map { document in
                    Item(shoutText: document.get("shout") as? String,
                         postedBy:  document.get("postedBy") as? String,
                         date: document.get("date") as? String,
                         pollQuestions: document.get("pollQuestions") as? [String] ?? [])
                }
                
                self.receivedCollectionView.reloadData()
                
            } else {
                print("GET POSTED MESSAGES no data")
            }
            
        }
    }
}

稍后,您可以访问此数据:

self.items[itemIndex].pollQuestions[pollQuestionIndex]

推荐阅读