如何过滤Firebase数据库中的数据?

时间:2017-03-10 18:11:12

标签: swift firebase-realtime-database

如何更改网络文件和fetchAllPosts中的homeviewController功能,以便我只从数据库中获得与我正在关注的用户的UID匹配的帖子。 UID,所以我的Feed中充满了我关注的用户帖子?

以下是介绍如何在数据库中创建新关注者的参考资料:

let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String)

以下是Firebase数据库中的帖子结构

posts
  -Ke4gQKIbow10WdLYMTL (generated key)
      postDate: 
      postId: 
      postPicUrl: 
      postText: 
      postTit: 
      type: 
      uid: looking to match this

以下是网络文件

中的当前fetchAllPosts函数
func fetchAllPosts(completion: @escaping ([Post])->()) {

    let postRef = self.dataBaseRef.child("posts")
    postRef.observe(.value, with: { (posts) in

        var resultsArray = [Post]()
        for post in posts.children {

            let post = Post(snapshot: post as! FIRDataSnapshot)
            resultsArray.append(post)

        }

        completion(resultsArray)

    }) { (error) in
        print(error.localizedDescription)
    }

    }

这是homeViewController中的fetchAllPosts函数

private func fetchAllPosts(){
    authService.fetchAllPosts {(posts) in
        self.postsArray = posts
        self.postsArray.sort(by: { (post1, post2) -> Bool in
            Int(post1.postDate) > Int(post2.postDate)
        })

        self.tableView.reloadData()
    }
}

这是我的swift文件中的帖子结构:

struct Post {


    var username: String!
    var uid: String!
    var postId: String!
    var type: String
    var postText: String
    var postTit: String
    var postPicUrl: String!
    var postDate: NSNumber!
    var ref: FIRDatabaseReference!
    var key: String = ""

    init(postId: String,postText: String, postTit:String, postDate:NSNumber, postPicUrl: String?, type:String, uid: String, key: String = ""){

        self.postText = postText
        self.postTit = postTit
        self.postPicUrl = postPicUrl
        self.type = type
        self.uid = uid
        self.postDate = postDate
        self.postId = postId

    }

    init(snapshot: FIRDataSnapshot){

        self.ref = snapshot.ref
        self.key = snapshot.key
        self.postId = (snapshot.value! as! NSDictionary)["postId"] as! String
        self.type = (snapshot.value! as! NSDictionary)["type"] as! String
        self.postPicUrl = (snapshot.value! as! NSDictionary)["postPicUrl"] as! String
        self.postDate = (snapshot.value! as! NSDictionary)["postDate"] as! NSNumber
        self.postTit = (snapshot.value! as! NSDictionary)["postTit"] as! String
        self.uid = (snapshot.value! as! NSDictionary)["uid"] as! String
        self.postText = (snapshot.value! as! NSDictionary)["postText"] as! String
    }


    func toAnyObject() -> [String: Any] {
        return ["postId":self.postId,"type":self.type, "postPicUrl":self.postPicUrl,"postDate":self.postDate, "postTit":self.postTit,"uid": self.uid, "postText":self.postText,]
    }
}

1 个答案:

答案 0 :(得分:0)

你用过这个吗;

    postRef.queryOrdered(byChild: "uid").queryEqual(toValue: yourUid).observe(.value, with: {
                    snapshot in

                if let shot = snapshot {
                    let post = Post(snapshot: post as! FIRDataSnapshot)
                }

    }

这会返回您要查找的数据。