我想做的是调用某个帖子 ID:
根据在 UITableView 上点击哪个单元格,并在不同的 ViewController 上显示来自它的数据。目前,每个帖子都有不同的 ID 以及上图中显示的数据
我见过这个方法用于根据ID调用用户名:
let uid = FIRAuth.auth()?.currentUser?.uid
FIRDatabase.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.userLabel.text = dictionary["username"] as? String
}
})
是否可以使用上面屏幕截图中所示的帖子 ID 来执行此操作?如果是这样,最简单的方法是什么?
最佳答案
好吧,我简化了代码,以便更容易解释。
首先在您的方法之外创建一个名为 postID
的变量,并将其设置为 FIRDatabase.database().reference().childByAutoID()
让 postID = UUID().uuidString
然后在您的 postObject 中添加另一组并设置您之前声明的变量 "postID": postID
let postObject: Dictionary<String, Any> = [
"uid": uid,
"title": title,
"price": price,
"rating": rating,
"description": description,
"username" : username,
"image": self.imageFileName,
"postID": self.postID
]
最后替换 FIRDatabase.database().reference().child("posts").childByAutoID().setValue(postObject)
使用 FIRDatabase.database().reference().child("posts").child(self.postID).setValue(postObject)
关于ios - 如何从 Firebase 数据库调用帖子 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42238643/