我正在尝试获取Firebase数据库中所有用户帖子的第一张图片,但是代码仅在第一位用户的帖子中运行,然后对于所有其他用户,它不会运行下面的代码块:含义为如果让为零,那我该如何解决呢?
更新我已经确定该错误发生在let imageDataL = try? Data(contentsOf: imageUrl!)
上,但是不确定如何更改它以使其正常工作。
只能运行一次的块:
if let imageUrlString = i.value as? [String:AnyObject], let postUrl = imageUrlString["image1"] as? String {
print("Description: inside the if let imageUrlString = i.value ")
self.feedArray.append(Post(fetchedImageURL: postUrl))
if let imageUrl = URL(string: postUrl), let imageDataL = try? Data(contentsOf: imageUrl), let image = UIImage(data: imageDataL) {
print("Description: inside the if let imageUrl = URL(string: postUrl)")
print("Description: img url's of posts: \(imageUrl)")
self.tableData.append(UserImage(image: image, postNum: p.key, userID: user))
self.tableView.reloadData()
} else {print("users had no posts, was nil")}
}
全功能:
func fetchAllUserFristImage() {
print("Description: calling of fetchAllUserFristImage()")
Database.database().reference().child("Posts").observe(.childAdded, with: {(snapshot) in
if snapshot.value as? [String: AnyObject] != nil {
let user = snapshot.key
print("Description: calling of snapshot.value is not nil ")
self.databaseRef = Database.database().reference()
let usersPostRef2 = self.databaseRef.child("Posts").child(user)
usersPostRef2.observe(.value, with: {(postXSnapshots) in
if let postDictionary2 = postXSnapshots.value as? [String:AnyObject] {
for (p) in postDictionary2 {
if let posts = p.value as? [String:AnyObject] {
print("Description: posts has value of: \(posts)")
//to get back to where i was delete the below for i
for (i) in posts {
if let imageUrlString = i.value as? [String:AnyObject], let postUrl = imageUrlString["image1"] as? String {
print("Description: inside the if let imageUrlString = i.value ")
self.feedArray.append(Post(fetchedImageURL: postUrl))
if let imageUrl = URL(string: postUrl), let imageDataL = try? Data(contentsOf: imageUrl), let image = UIImage(data: imageDataL) {
print("Description: inside the if let imageUrl = URL(string: postUrl)")
print("Description: img url's of posts: \(imageUrl)")
self.tableData.append(UserImage(image: image, postNum: p.key, userID: user))
self.tableView.reloadData()
} else {print("users had no posts, was nil")}
}
}
}
}
}
})
//below shud stay same
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
})
}
我的数据库结构:
{
"Posts" : {
"7AM0CeJ71tYON9NejziFZto16fk2" : {
"post1" : {
"ImageUrl" : {
"image1" : "https://URLHere",
"image2" : "https://URLHere"
}
}
},
"Q4qiUCjN5bNXdPkrzYMQFGe79oH3" : {
"post1" : {
"ImageUrl" : {
"image1" : "https://URLHere",
"image2" : "https://URLHere"
}
}
}
},
"users" : {
"7AM0CeJ71tYON9NejziFZto16fk2" : {
"email" : "dhhfhfhffhdh@hdhdhdjdh.hehdhdhdh",
"password" : "dhdhdhhdhdhdhdhdhfhf"
},
"9S9W4EM52PZn23smnhNytOWvvIt2" : {
"email" : "hdhddhhddhdh@hdhddhdh.hedhhdhddyd",
"password" : "dhdhdhhdhdhdhdhdhf"
},
"B3DnljxZMmXd5Hrq1qTKQl08s1a2" : {
"email" : "fhfhfhhfhfhf@3737476446.38384747",
"password" : "hehdhdhfhdhfhfhfhdhdhdhdhdhdhdhdhf"
},
"DuLp2XUBkkbQ223JHwmN6nzJR942" : {
"email" : "hrhdhdhdfhfh@hdhddhdh.hdhdhf",
"password" : "ddhdhhddjhfhfg"
},
"Q4qiUCjN5bNXdPkrzYMQFGe79oH3" : {
"email" : "nehdhdhdhddhfh@hddhdhdh.hdhdhdh",
"password" : "dhhdhdhfhdhdhfhfhdfb"
},
}
}
答案 0 :(得分:0)
以下代码将遍历Posts中的子节点,为每个帖子打印uid,然后为每个帖子打印图像名称和url。请注意var self 指向我的Firebase。
func fetchAllUsersImages() {
self.ref.child("Posts").observe(.childAdded, with: { snapshot in
let uid = snapshot.key
let postSnap = snapshot.childSnapshot(forPath: "post1")
let imageUrlSnap = postSnap.childSnapshot(forPath: "ImageUrl")
guard let allImages = imageUrlSnap.children.allObjects as? [DataSnapshot] else { return }
for url in allImages {
let imageName = url.key
let path = url.value as! String
print(uid, imageName, path)
}
print()
})
}
以及基于您的结构的输出
7AM0CeJ71tYON9NejziFZto16fk2 image1 https://URLHere
7AM0CeJ71tYON9NejziFZto16fk2 image2 https://URLHere
Q4qiUCjN5bNXdPkrzYMQFGe79oH3 image1 https://URLHere
Q4qiUCjN5bNXdPkrzYMQFGe79oH3 image2 https://URLHere