【问题标题】:Can't load my current user's posts from Firebase - Swift无法从 Firebase 加载我当前用户的帖子 - Swift
【发布时间】:2018-10-25 03:03:56
【问题描述】:

我想从我的 Firebase 数据库中加载我当前用户的帖子。

这是我的数据库架构: 我已经成功地将所有帖子加载到主视图中,但是当我尝试复制并在此处使用相同的方法进行必要的更改并为当前用户的帖子使用 uid 时,它没有显示任何内容......已经没有想法了至于问题可能是什么,可以使用一些帮助。

这是来自 ProfileUserPosts swift 文件的屏幕截图:

    //VARS
    var postsuser = [ProfileUserPosts]()    

@objc func observeUserPosts() {
        let uid = Auth.auth().currentUser?.uid
        let postsRef = Database.database().reference().child("posts").child("author")
         postsRef.queryOrdered(byChild: "userid").queryEqual(toValue: uid!).observe(.value) { (snapshot) in

            var tempPost = [ProfileUserPosts]()

            for child in snapshot.children {
                if let childSnapshot = child as? DataSnapshot {

                    let dict = childSnapshot.value as? [String: Any]

                    //Post Picture
                    let photoUrl = dict!["photoUrl"] as? String
                    let url = URL(string: photoUrl!)

                    //Info Post
                    let comments = dict!["comments"] as? String
                    let city = dict!["city"] as? String
                    let municipality = dict!["municipality"] as? String
                    let breed = dict!["breed"] as? String
                    let phoneuser = dict!["phone"] as? String
                    let postType = dict!["postType"] as? String
                    let petType = dict!["petType"] as? String
                    let gender = dict!["gender"] as? String
                    let timestampadoption = dict!["timestamp"] as? Double
                    let date = Date(timeIntervalSince1970: timestampadoption!/1000)

                    let post = ProfileUserPosts(breed: breed!, phone: phoneuser!, photoUrl: url!, city: city!, municipality: municipality!, petType: petType!, gender: gender!, timestamp: date, postType: postType!, comments: comments!)

                    tempPost.insert(post, at: 0)
                }

                DispatchQueue.main.async {
                    self.postsuser = tempPost
                    self.postsCollectionView.reloadData()
                    self.refresher.endRefreshing()
                }
            }
        }
    }

这里是我用 numberOfItemsInSection 和 cellForItem 加载 CollectionView 的地方

extension ProfileViewController: UICollectionViewDataSource,UICollectionViewDelegate {


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return postsuser.count

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell

        cell.set(post: postsuser[indexPath.row])

        return cell
    }

}

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    好吧,根据您的数据库结构,我的猜测是您访问的路径无效。 当您引用Database.database().reference().child("posts").child("author") 时,您是在说posts 有一个author 的直系子代,这不是真的,它们是post id 的直系子代,然后获取相应的post ,您可以获得author,因此在这种情况下,您必须遍历所有帖子。

    所以,要获取所有帖子:

    let postsRef = Database.database().reference().child("posts")

    那么你会有一个 对象非常重要 -这不是数组)以 ID 作为键的帖子,那么你会遍历它们并获取您想要的相应作者。我不会详细介绍您的实现,只是为了澄清我的意思:

    let specificPostAuthor = postsRef.child("LOj1UZZgUnKa28xVtht").child("author")

    将返回相应的帖子作者

    【讨论】:

    • 对不起,Guilherme,我是自学编程的,而且还是新手,我不太理解你的回复,你能进一步解释一下吗:) ....
    • 当然,我会尝试重新制定我的答案,但首先很高兴知道您的(snapshot) 返回什么,所以我不会在这里搞砸答案
    • 我实际上没有看到任何快照:o
    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 2020-08-05
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多