搜索有关用户订单的信息,并使用Firebase按日期(从最新到最新)排序

时间:2018-11-22 14:15:49

标签: ios swift firebase firebase-realtime-database

我在Firebase中有这样的订单表 enter image description here

并且在我的iOS应用中,我想显示活动用户的订单,并想按(CreateDate)排序以显示最新的

我正在使用此代码,仅显示用户订单,而不按日期排序

 if   (Auth.auth().currentUser != nil) {
        let user = Auth.auth().currentUser
        if let user = user {
            uid = user.uid

            print("uid = \(uid)")


            // get order details
            let ordersRef = rootRef.child("orders")
            ordersRef.queryOrdered(byChild: "clientuid_status").queryEqual(toValue: "\(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in

                let snabshotvalue = snapshot.value as? [String : AnyObject]
                let dateorder = snabshotvalue!["orderDate"]
                let tecname = snabshotvalue!["tecname"]
                let theproblem = snabshotvalue!["theproblem"]
                let homeadress = snabshotvalue!["homeadress"]
                let clintlat = snabshotvalue!["clintlat"]
                let clintlong = snabshotvalue!["clintlong"]
                let clintuid = snabshotvalue!["clintuid"]
                let tecuid =  snabshotvalue!["tecuid"]
                let tecToken  = snabshotvalue!["tecToken"]
                let status = snabshotvalue!["status"]
                let servicetable = snabshotvalue!["servicetable"]

                let snashotkey = snapshot.key as String
                print("firstname: \(tecname) ")

                self.dateArray.append(dateorder as? String)
                self.tecnameArray.append(tecname as? String)
                self.problemArray.append(theproblem as? String)
                self.homeadressArray.append(homeadress as? String)
                self.clintlatArray.append(clintlat as? String)
                self.clintlongArray.append(clintlong as? String)
                self.tecuidArray.append(tecuid as? String)
                self.tecTokenArray.append(tecToken as? String)
                self.orderuidArray.append(snashotkey )
                self.clintidArray.append(clintuid as? String)
                self.statusArray.append(status as? String)
                self.servicetableArray.append(servicetable as? String)

                self.getTechRatin(teckUid: tecuid as! String)

                self.tableView.reloadData()
            })


            }

我已将票发送到Firebase支持,他们告诉我使用此代码:

    let orderOrdersByCreatedDate= (ref.child("orders").child(getUid())).queryOrdered(byChild: "createDate").queryLimited(toLast: 1)

当我尝试使用您的代码时,出现此错误: 使用未解决的标识符“ getUid”

我不知道如何使用getUid()

1 个答案:

答案 0 :(得分:0)

最后一段代码是错误的。要按creadeDate对订单进行排序,您需要:

ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...

但是Firebase数据库查询只能对单个属性进行排序/过滤,这意味着您不能像现在一样对clientuid_status进行过滤。

在许多情况下,可以将要过滤的值组合为单个(合成)属性。您似乎已经使用clientuid_status完成了此操作,并且要允许同时过滤和按日期排序,请将其扩展到clientuid_status_creadeDate并对其进行排序/过滤。

有关此方法和其他方法的示例,请在此处查看我的答案:http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase