【问题标题】:Display Firebase data based on username根据用户名显示 Firebase 数据
【发布时间】:2017-02-16 01:27:03
【问题描述】:

我目前有一个 UITableView 在单元格中显示 Firebase 数据库中的帖子。还有另一个 UITableView,我想只显示登录用户的帖子。

这是我目前在我的 UserPostViewController 中拥有的:

func loadData() {
    let uid = FIRAuth.auth()?.currentUser?.uid
    FIRDatabase.database().reference().child("posts").child(uid!).observeSingleEvent(of: .value, with: {
        (snapshot) in
        if let postsDictionary = snapshot.value as? [String: AnyObject] {
            for post in postsDictionary {
                self.userPosts.add(post.value)
            }
            self.userPostsTableView.reloadData()
        }
    })
}

// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // pass any object as parameter, i.e. the tapped row
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.userPosts.count
}

// Displays posts in postsTableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! PostTableViewCell
    // Configure the cell...
    let post = self.userPosts[indexPath.row] as! [String: AnyObject]
    cell.titleLabel.text = post["title"] as? String
    cell.priceLabel.text = post["price"] as? String
    if let imageName = post["image"] as? String {
        let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil {
            let image = UIImage(data: data!)
            cell.titleLabel.alpha = 0
            cell.contentTextView.alpha = 0
            cell.postImageView.alpha = 0
            UIView.animate(withDuration: 0.4, animations: {
                cell.titleLabel.alpha = 1
                cell.contentTextView.alpha = 1
                cell.postImageView.alpha = 1
            })
        } else {
            print("Error occured during image download: \(error?.localizedDescription)")
            }
        })
    }
    return cell
}

这也是我在 Firebase 中的意思的图片:

【问题讨论】:

    标签: ios swift uitableview firebase firebase-realtime-database


    【解决方案1】:

    Firebase 是一个 NoSQL 数据库。试图在上面投射 SQL 实践,会让你感到悲伤。

    不要存储一长串帖子然后使用查询进行过滤,而是将每个用户的帖子存储在用户特定的节点下。即

    users
      $uid
        username: "gabe"
    posts
      $uid
        -K12348: {
          description: "...."
          image: "...."
        }
    

    现在您可以通过直接访问读取访问特定用户的帖子,而不是在不断增长的列表中进行查询:

    FIRDatabase.database().reference().child("posts").child(uid!).observeSingleEvent(of: .value, with: {
        (snapshot) in
        if let postsDictionary = snapshot.value as? [String: AnyObject] {
            for post in postsDictionary {
                self.userPosts.add(post.value)
            }
            self.userPostsTableView.reloadData()
        }
    

    下次请将您的 JSON 作为文本发布,以便我可以将其复制/粘贴到我的答案中。

    我建议阅读NoSQL data modeling 和查看Firebase for SQL developers

    【讨论】:

    • 我完全理解你的想法,但是如果我要在我的数据库中实现它,就无法调用 UITableView 中的所有帖子
    • 没错。这就是您为每个用例建模数据的原因。通常,您最终会在多个地方复制数据。这在 NoSQL 数据库上很正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    相关资源
    最近更新 更多