Swift firebase获取按两个子值排序的数据

Swift firebase获取按两个子值排序的数据,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我正在尝试使用swift和firebase在搜索栏中查询搜索词的多个子值。目前,我只查询“title”属性,它工作得非常好,但是有没有一种方法可以查询“title”和“category”并显示结果 func fetchSearchedPosts(searchTerm: String) { self.collectionView.refreshControl?.endRefreshing() let ref = Database.database().reference().ch

我正在尝试使用swift和firebase在搜索栏中查询搜索词的多个子值。目前,我只查询“title”属性,它工作得非常好,但是有没有一种方法可以查询“title”和“category”并显示结果

func fetchSearchedPosts(searchTerm: String) {

    self.collectionView.refreshControl?.endRefreshing()
    let ref = Database.database().reference().child("posts").queryOrdered(byChild: "title").queryStarting(atValue: searchTerm).queryEnding(atValue: "\(searchTerm)\u{f8ff}")
   ref.observeSingleEvent(of: .value) { (snapshot) in

    if !snapshot.exists() { return }

    guard let dictionaries = snapshot.value as? [String: Any] else { return }
    self.posts.removeAll()
    let g = DispatchGroup()   ///// 1

    dictionaries.forEach({ (key, value) in

        guard let postDictionary = value as? [String: Any] else { return }
        guard let uid = postDictionary["uid"] as? String else { return }

        g.enter()   ///// 2
        Database.fetchUserWithUID(uid: uid, completion: { (user) in

            let post = Post(postId: key, user: user, dictionary: postDictionary)

            let nowTimeStamp = Date().timeIntervalSince1970
            let dateTime = post.endTimeDate
            let timeStamp = dateTime.timeIntervalSince1970

                if nowTimeStamp < timeStamp {

                    post.id = key
                    self.posts.append(post)

                } else {
                    g.leave()   ///// 3.a
                    return
                }
            g.leave()   ///// 3.b
        })
    })

    g.notify(queue:.main) {    ///// 4
      self.posts.sort(by: { (post1, post2) -> Bool in
           return post1.title.compare(post2.title) == .orderedAscending
       })
       self.collectionView.reloadData()
     }
  }
}
func fetchSearchedPosts(searchTerm:String){
self.collectionView.refreshControl?.endRefresh()
让ref=Database.Database().reference().child(“posts”).queryOrdered(byChild:“title”).queryStart(atValue:searchTerm)。queryEnding(atValue:“\(searchTerm)\u{f8ff}”)
中的ref.observeSingleEvent(of:.值){(快照)
如果!snapshot.exists(){return}
guard let dictionaries=snapshot.value as?[String:Any]else{return}
self.posts.removeAll()
设g=DispatchGroup()//1
字典.forEach({(键,值)在
guard let postDictionary=值为?[String:Any]else{return}
guard let uid=postDictionary[“uid”]作为字符串else{return}
g、 回车()///2
Database.fetchUserWithUID(uid:uid,完成:{(用户)在
让post=post(postId:key,user:user,dictionary:postdirectionary)
让nowTimeStamp=Date().timeIntervalSince1970
让dateTime=post.endTimeDate
让timeStamp=dateTime.timeIntervalSince1970
如果nowTimeStampBool-in
返回post1.title.compare(post2.title)=.orderedAscending
})
self.collectionView.reloadData()
}
}
}

按照firebase Firestore文档中的查询限制,一次在多个字段上执行firebase查询是不可能的

Cloud Firestore provides limited support for logical OR queries. The in and array 
contains-any operators support a logical OR of up to 10 equality (==) or array-contains 
conditions on a single field. For other cases, create a separate query for each OR 
condition and merge the query results in your app.

请参阅查询限制部分

现在看看这些限制,你能做的是:

一,

二,

还有其他方法可以优化过滤过程,但对于小型数据库来说,这不会是一个问题


希望这有帮助

Firebase数据库查询只能对单个属性进行排序/筛选。在许多情况下,可以将要筛选的值组合到单个(合成)特性中。有关此方法和其他方法的示例,请参见我的回答:
query multiple times for each fields and merge the outputs.
 If you have less fields to query like in your case 2(title and category) query 
 initially using the field that would give you the smallest result and then perform a 
 simple search on that output based on the other field in order to get the desired output.