【发布时间】:2018-11-03 00:51:57
【问题描述】:
我的代码中有以下函数,它从 firebase 获取图像,它可以正确执行所有操作,但是它复制了 2 个图像(在这种情况下)它的获取。
编辑:(下面的情况可能并非如此,我相信这只是基于尝试多个看似有效的修复并没有解决问题的事实)。
这似乎是由于self.tableView.reloadData() 的放置而引起的,但是我尝试了许多放置都失败了。
func fetchAllUsersImages() {
print("inside func")
self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in
print(snapshot.value)
if let snapShotValue = snapshot.value as? [String: String] {
for (_, value) in snapShotValue {
print(value)
if let imageURL = URL(string: value) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
let ImageObject = Image()
ImageObject.image = image
self.arrayOfImgObj.append(ImageObject)
self.tableView.reloadData()
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
print("hfjdsaklhjfksahalh")
})
print(arrayOfImgObj.count)
}
这是控制台的输出:
Description: post1
Description: oosJPm4MEnWWis5p7p1unCbZxQH3
inside func 0 Optional({
image1 = "https://URLIsHere";
image2 = "https://URLIsHere";
})
https://URLIsHere
https://URLIsHere image url here
https://URLIsHere
https://URLIsHere image url here
hfjdsaklhjfksahalh
【问题讨论】:
标签: ios swift firebase uitableview firebase-realtime-database