ios - 如何从 Firebase 数据库调用帖子 ID

标签 ios swift uitableview firebase firebase-realtime-database

我想做的是调用某个帖子 ID:

Image

根据在 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/

相关文章:

objective-c - 在 Swift 中创建等效的 Objective-C Getter 和 Setter

ios - UITableView selectRowAtIndexPath 断言失败

ios - Facebook iOS 应用程序的无效应用程序 ID

ios - 使用 NSFetchedResultsController 移动行的方法及其委托(delegate)不起作用

Swift 帮助从 future 日期中提取月份值

ios - 如何在 UItableview IOS7 中播放 youtube 视频

ios - UITableViewCell 没有调整到 UIImageView

ios - 使用 NSURLConnection 上传时将进度设置为 UIProgressView

iphone - 未显式调用 Objective-C 版本

swift - 使用 mask 层使 UIView 的某些部分透明