如何在 Swift 中使 Firestore 查询可变

How to make Firestore query mutable in Swift

提问人:Guri 提问时间:9/26/2021 更新时间:9/28/2021 访问量:66

问:

我有一个 android 项目,其中有一个可变的 firestore 查询,它完全符合我的要求,但在 swift 中不可能做同样的事情,因为 firestore 查询在 swift 中是不可变的。基本上,我正在根据一些过滤器参数过滤 firestore 数据。我希望如果用户只应用一些过滤器,那么该过滤器将添加到查询中,否则我不想在查询中添加该过滤器。

        
        var query = db.collection("admissions")
        if (self.txt_search.text?.count ?? 0 > 0){
                query = query.whereField("keyword", arrayContains: self.txt_search.text ?? "")
            }
            if(self.lbl_state.text != "State" && self.lbl_state.text != "" && self.lbl_state.text != "Any"){
                query = query.whereField("state", isEqualTo: self.lbl_state.text ?? "")
            }
            if(self.lbl_level.text != "Level" && self.lbl_level.text != "" && self.lbl_level.text != "Any"){
                query = query.whereField("type", isEqualTo: self.lbl_level.text ?? "")
            }
            if(self.lbl_field.text != "Field" && self.lbl_field.text != "" && self.lbl_field.text != "Any"){
                query = query.whereField("field_broad", isEqualTo: cell.lbl_field.text ?? "")
            }
            if(self.lbl_provider.text != "Provider" && self.lbl_provider.text != "" && !self.selectedUniversity.contains("Any")){
                query = query.whereField("university", in: self.selectedUniversity)
            }
            if(self.lbl_duration.text != "Duration" && self.lbl_duration.text != "" && self.lbl_duration.text != "Any"){
                if(self.lbl_duration.text == "<1 year"){
                    query = query.whereField("duration", isLessThanOrEqualTo: 365)
                }else if(cell.lbl_duration.text == "1-2 year"){
                    query = query.whereField("duration", isGreaterThanOrEqualTo: 365).whereField("duration", isLessThanOrEqualTo: 365 * 2)
                }else{
                    query = query.whereField("duration", isGreaterThanOrEqualTo: 365 * 2)
                }
            }
        query.order(by: "duration").order(by: "id").limit(to: 50)
        
        query.getDocuments() { (querySnapshot, err) in
            AFWrapper.stopAnimating()
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                  }   
            }
       }
迅速 Firebase 谷歌云火存储 可变

评论

0赞 El Tomato 9/26/2021
解释一下你的意思“如果用户只应用一些过滤器,那么该过滤器将在查询中添加,否则我不想在查询中添加该过滤器”这在英语中不是一个有意义的句子。

答:

1赞 trndjc 9/28/2021 #1

您将错误的类型归因于查询;您无意中将其作为集合引用。有两种方法可以解决此问题。首先是创建一个可选的:Query

var query: Query? // define the query's type

if (self.txt_search.text?.count ?? 0 > 0) {
    query = db.collection("admissions").whereField("keyword", arrayContains: self.txt_search.text ?? "") // define the query instance
}

query = query?.order(by: "duration").order(by: "id").limit(to: 50) // append the query

query?.getDocuments() { (querySnapshot, err) in
    // execute the query
}

另一种是直接定义:Query

var query = db.collection("admissions").whereField("someUniversalField", isEqualTo: someValue).limit(to: 50) // define the query outright

if (self.txt_search.text?.count ?? 0 > 0) {
    query = query.whereField("keyword", arrayContains: self.txt_search.text ?? "") // append the query
}

query.getDocuments() { (querySnapshot, err) in
    // execute the query
}