问题
Hi I am wanting to populate some data from my firebase to my tableView but do not really understand how to go about doing it. Below is a snippet from my code detailing where the data is coming from and the current tableView arrangement I have:
Edited Attempt
func fetchPosts(completion:@escaping (_ theposts:[Posts])->()){
oldPostQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value, with: { snapshot in
var tempPosts = [Posts]()
let lastPost = self.theposts.last
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let data = childSnapshot.value as? [String:Any],
let theposts = Posts.parse(childSnapshot.key, data),
childSnapshot.key != lastPost?.id {
guard let user = Auth.auth().currentUser else { return }
let uid = user.uid
let ref = Database.database().reference().child("posts")
let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
query.observeSingleEvent(of: .value, with: { snapshot in
let allPosts = snapshot.children.allObjects as! [DataSnapshot]
for postSnap in allPosts {
let textUser = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
print(textUser)
//same as above
}
})
tempPosts.insert(theposts, at: 0)
}
}
return completion(tempPosts)
} )
}
I am aware that the tableView I am using is empty cell.set(theposts: theposts[indexPath.row]) but I was using this prior as this is the only way I know how to get the data. The information appears in my console but does not populate on the display.
来源:https://stackoverflow.com/questions/62199497/populating-firebase-information-into-tableview