我正在尝试从Firebase加载数据,我过去使用过以下代码但是我添加了一个额外的子代,因此以下代码现在不起作用。我迷路了。任何帮助将不胜感激。
图像是UIImageView
let databaseRef = Database.database().reference()
databaseRef.queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let content2 = (snapshot.value as? NSDictionary)?["imageWebsite"] as? String ?? ""
let content3 = (snapshot.value as? NSDictionary)?["infoWebsite"] as? String ?? ""
self.imgURL = content2
self.infoURL = content3
})
print(imgURL)
image.setImageFromURl(stringImageUrl: imgURL)
答案 0 :(得分:1)
通过在数据库查询中放置setImageFromURL
func,它会在完成查询之前执行代码,确保加载objects
。
let databaseRef = Database.database().reference()
databaseRef.queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let content2 = (snapshot.value as? NSDictionary)?["imageWebsite4"] as? String ?? ""
let content3 = (snapshot.value as? NSDictionary)?["infoWebsite4"] as? String ?? ""
self.imgURL = content2
self.infoURL = content3
print(content2)
print(self.imgURL)
self.image.setImageFromURl(stringImageUrl: self.imgURL)
})