arrays - 如何使用 Swift 从 Firestore 检索数组中保存的数据
问题描述
这似乎应该是一件很简单的事情,但似乎找不到解决方案。我在 Firestore 中有一些数据保存在一个数组中,我需要获取这些数据并将其放入我的 swift 应用程序中的二维数组中。我很菜鸟所以提前道歉!
这是我用来查询我的文档的代码,然后循环遍历结果,提取数据
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 数组的数据,用于我循环遍历的每个文档(这有意义吗?)。
一直在搜索,并看到对地图等的引用,但没有找到解决方案。
任何帮助表示赞赏!
解决方案
与其将每个属性存储在单独的数组中,不如考虑使用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]
推荐阅读
- c# - 当一个从 DBContextBase 和另一个从 DomainDBContext 读取时,从 2 个不同项目中的类访问 DBContext
- java - WSO2 API Manager v1.10.0内置tomcat版本如何升级?
- notepad++ - 如何一次注释掉 shell 脚本中的特定行
- java - 更改集 [liquibasefile] 失败。错误:表 [表名] 不存在 [失败的 SQL:INSERT INTO [表名] (name) VALUES ('ROLE_USER')]
- android - Switch Case 语句是否适用于自动生成的 android 资源 ID?
- javascript - 单击停止按钮后如何删除录制图标?
- firebase - 如何创建具有 Firebase 功能的新用户?
- xamarin.forms - Xamarin.Forms 应用程序中 BroadcastReceiver 中的 Application.Current.Properties
- powershell - 为什么是Get-DnsClientServerAddress | 选择 AddressFamily 输出不是 IPv4 和 IPv6
- javascript - 如何在 jquery 函数中插入 php 代码