gpt4 book ai didi

swift - 使用 Swift 3.0 从随机子路径从 Firebase 检索数据

转载 作者:行者123 更新时间:2023-11-28 08:23:23 25 4
gpt4 key购买 nike

我正在尝试使用现有的字符串数组作为引用,从具有随机文件夹的目录中检索项目数组

我的数据是这样的:

 Items
- RandomID
-title : "text"
-subtitle: "text"

到目前为止,这是我尝试过的方法,但它不起作用:

var array = [String]() //array to use as reference
var returnedItems = [Item]() //array of item objects

func retrieveData()
{
for i in array
{
let ref = main.child("Items")
let query = ref.queryEqual(toValue: i)
query.observeSingleEvent(of: .value, with: { (snapshot) in

let item = Item!

if snapshot.hasChild("title")
{
item.title = (snapshot.value as! NSDictionary)["title"] as? String
}
if snapshot.hasChild("subtitle")
{
item.subtitle = (snapshot.value as! NSDictionary)["subtitle"] as? String
}

returnedItems.append(item)
self.tableView.reloadData()
print("Item: \(self.returnedItems.map { $0.title})")

})
}
}

任何帮助将不胜感激!提前致谢;)

最佳答案

如果你想检索所有的 child ,你可以只使用一个监听器(模仿 this example in the documentation ):

var array = [String]() //array to use as reference
var returnedItems = [Item]() //array of item objects

func retrieveData() {
query.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let item = Item!
if child.hasChild("title") {
item.title = (snapshot.value as! NSDictionary)["title"] as? String
}
if child.hasChild("subtitle") {
item.subtitle = (snapshot.value as! NSDictionary)["subtitle"] as? String
}

returnedItems.append(item)
self.tableView.reloadData()
print("Item: \(self.returnedItems.map { $0.title})")
})
}
// returnedItems will still be empty here, since the data hasn't
// been loaded yet. See the note after this code snippet.
}

但请注意,此处读取数据仍将异步发生,因此当 retrieveData 返回时,returnedItems 仍为空。

关于swift - 使用 Swift 3.0 从随机子路径从 Firebase 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com