打开Firebase数据库Feed

时间:2017-08-15 15:21:22

标签: ios swift firebase firebase-realtime-database

是否有人建议如何在将新元素发布到Firebase数据库时摆脱用户义务?我想创建开放源,但这仅适用于一个特定用户。 谢谢!

该功能如下:

   func fetchPosts(){

    let ref = Database.database().reference()
    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot inlet users = snapshot.value as! [String : AnyObject]

我在此看到的主要问题是:

        for (_,value) in users {
            if let uid = value["uid"] as? String {
                if uid == Auth.auth().currentUser?.uid {
                    if let followingUsers = value["following"] as? [String : String]{
                        for (_,user) in followingUsers{
                            self.following.append(user)
                        }
                    }
                    self.following.append(Auth.auth().currentUser!.uid)

                    ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in


                        let postsSnap = snap.value as! [String : AnyObject]

                        for (_,post) in postsSnap {
                            if let userID = post["userID"] as? String {
                                for each in self.following {
                                    if each == userID {

我想这会是一样的:

                                        let posst = Post()
                                        if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {

                                            posst.author = author
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            if let people = post["peopleWhoLike"] as? [String : AnyObject] {
                                                for (_,person) in people {
                                                    posst.peopleWhoLike.append(person as! String)
                                                }
                                            }

                                            self.posts.append(posst)
                                        }
                                    }
                                }

                                self.collectionview.reloadData()
                            }
                        }
                    })
                }
            }
        }

    })}

1 个答案:

答案 0 :(得分:0)

好的,如果我正确理解您的问题,您希望所有登录的用户都能看到所有帖子,而不仅仅是他们关注的用户的帖子。

如果确实如此,那么您无需检查用户关注的对象。您只想加载所有帖子。改变

for (_,post) in postsSnap {
     if let userID = post["userID"] as? String {
          for each in self.following {
               if each == userID {

  self.ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { snap in
                        if let postsSnap = snap.value as? [String: AnyObject] {
                            for(_, post) in postsSnap {
                                if let userID = post["userID"] as? String {
                                    for each in self.following {
                                        if each == userID {
                                            let posst = Post()

                                            if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let group = post["group"] as? Int {

                                                posst.author = author
                                                posst.likes = likes
                                                posst.pathToImage = pathToImage
                                                posst.postID = postID
                                                posst.userID = userID
                                                posst.fancyPostDescription = self.createAttributedString(author: author, postText: postDescription)
                                                posst.postDescription = author + ": " + postDescription
                                                posst.timestamp = timestamp
                                                posst.group = group
                                                posst.category = category
                                                posst.userWhoPostedLabel = self.createAttributedPostLabel(username: author, table: group, category: category)

                                                if let people = post["peopleWhoLike"] as? [String: AnyObject] {
                                                    for(_, person) in people {
                                                        posst.peopleWhoLike.append(person as! String)
                                                    }
                                                }
                                                self.posts.append(posst)
                                            } // end if let
                                        }
                                    }
                                    self.tableView.reloadData()
                                }
                            }
                        }
                    })
                    ref.removeAllObservers()

如果您希望任何人能够读取/写入您的数据库,您必须更新Firebase控制台中的规则。通过单击数据库然后单击规则来执行此操作。默认规则如下所示:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

将其更改为

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

我看了你正在关注的视频,他犯了很多错误。实际上,当您从Firebase请求数据时,您应该进行分页,即加载一定数量的帖子,然后根据滚动位置加载更多。