Swift 如何从Firebase数据库中筛选数据?

Swift 如何从Firebase数据库中筛选数据?,swift,firebase-realtime-database,Swift,Firebase Realtime Database,如何更改我的网络文件和homeviewController中的fetchAllPosts功能,以便我只从数据库中获取与我跟踪的用户的UID和post.UID匹配的帖子,从而使我的提要中充满我跟踪的用户的帖子 以下是我如何在数据库中创建新跟随者的参考: let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String) 以下

如何更改我的网络文件和
homeviewController
中的
fetchAllPosts
功能,以便我只从数据库中获取与我跟踪的用户的UID和post.UID匹配的帖子,从而使我的提要中充满我跟踪的用户的帖子

以下是我如何在数据库中创建新跟随者的参考:

let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String)
以下是Firebase数据库中的post结构

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,]
    }
}
你用过这个吗

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

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

    }
这将返回您正在查找的数据