如何使用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数组数据存入一个二维数组中(请问是否可以实现?)。我搜索了全网资料,也看到了map等相关用法,但始终没找到可行的解决方案,感谢大家的帮助!
解决方案
1. 调整数组变量类型
将存储投票问题的数组定义修改为二维字符串数组:
// 原定义示例 // var postedQuestionsArray: [String] = [] // 修改为 var postedQuestionsArray: [[String]] = []
2. 修改数组赋值逻辑
将每个文档读取到的单个数组追加到二维数组中,而非直接覆盖原有数组,同时为无对应字段的文档插入空数组占位,保证所有存储文档信息的数组下标一一对应:
// 原逻辑 /* if let pollQuestions = document.get("pollQuestions") as? [String] { self.postedQuestionsArray = pollQuestions } else { print("no array data") } */ // 替换为 if let pollQuestions = document.get("pollQuestions") as? [String] { self.postedQuestionsArray.append(pollQuestions) } else { self.postedQuestionsArray.append([]) print("no array data") }
3. 优化列表刷新逻辑
将self.receivedCollectionView.reloadData()移到for循环外部,避免每遍历一个文档就刷新一次列表,造成不必要的性能损耗。
优化后的完整代码
同时对可选值解包逻辑做了优化,避免强制解包风险:
fireStoreDatabase.collection("Posts").whereField("postedTo", arrayContains: userId).order(by: "date", descending: true).addSnapshotListener { (snapshot, error) in // 错误处理 if let error = error { print(error.localizedDescription) return } // 快照非空校验 guard let snapshot = snapshot, !snapshot.isEmpty else { print("GET POSTED MESSAGES no data") return } print("Posted data got") // 清空数组避免数据重复 self.postedShoutTextArray.removeAll(keepingCapacity: false) self.postedByArray.removeAll(keepingCapacity: false) self.postedDateArray.removeAll(keepingCapacity: false) self.postedQuestionsArray.removeAll(keepingCapacity: false) // 遍历所有文档 for document in snapshot.documents { // 普通字段读取,空值插入占位符保证下标对齐 let shoutText = document.get("shoutText") as? String ?? "" self.postedShoutTextArray.append(shoutText) let postedBy = document.get("postedBy") as? String ?? "" self.postedByArray.append(postedBy) let date = document.get("date") as? String ?? "" self.postedDateArray.append(date) // 数组字段读取 let pollQuestions = document.get("pollQuestions") as? [String] ?? [] self.postedQuestionsArray.append(pollQuestions) } // 所有数据处理完成后统一刷新列表 self.receivedCollectionView.reloadData() }
后续使用时,通过postedQuestionsArray[文档下标]即可获取对应文档的投票问题数组,通过postedQuestionsArray[文档下标][问题下标]可获取具体的问题内容。
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

