如何在Swift中的Firebase查询中应用多个过滤器?

时间:2017-08-01 09:07:30

标签: ios swift firebase firebase-realtime-database pagination

我正在尝试开发Taxi预订等应用程序并在Firebase上存储数据。

但是,我在从Firebase查询RideDetail(历史记录)的数据时遇到问题。

我想为特定的" customer_id"获取ride_detail。以分页形式。

我的Firebase DataStructure:

{
  "ride_details": {
    "NuEoP2WNPwigsbY1FQy9M150131918189233": {
      "customer_id": "tstebwLlf4OCRdWhNKO9XCO08xY2",
      "destination_address": "New Ranip\nNew Ranip\nAhmedabad\nGujarat 380081\nIndia",
      "destination_lang": 72.55924470000001,
      "destination_latg": 23.0930152,
      "discount": "10%",
      "driver_id": "cIyZQIJ7tsdvF1a9KpRrKucF2o62",
      "drop_time": "2017-07-29 09:12:21 +0000",
      "fare": "13.16 Rs.",
      "payment_time": 150149034812771,
      "pickup_time": "2017-07-29 09:10:38 +0000",
      "priceperkm": "10.00 Rs.",
      "ride_confirm_time": "2017-07-29 09:06:21 +0000",
      "source_address": "Vastrapur\nVastrapur\nAhmedabad\nGujarat\nIndia",
      "source_lang": 72.5293244,
      "source_latg": 23.0350073,
      "tax": "10%"
    },
    "RH0oZ0Ypbkur3wJM3HMvM150147833457957": {
      "customer_id": "aYQFbwLlf4OCRdWhNKO9XCO08xY2",
      "destination_address": "Sarovar Park Plaza Hotels and Resorts Private Limted\nNo 1\nSector 10\nCBD Belapur\nWadala West\nWadala\nMumbai\nMaharashtra 400614\nIndia",
      "destination_lang": 72.8561644,
      "destination_latg": 19.0176147,
      "discount": 0,
      "driver_id": "cIyZQIJ7tsdvF1a9KpRrKucF2o62",
      "drop_time": "",
      "fare": 0,
      "payment_time": 150149034812772,
      "pickup_time": "",
      "priceperkm": 0,
      "ride_confirm_time": "2017-07-31 05:18:54 +0000",
      "source_address": "Smokin Joe's Fresh Pizza\nShop No. 2\n3\nGround Floor\nAbhiman II\nWadala West\nThane West\nMumbai\nMaharashtra 400602\nIndia",
      "source_lang": 72.8561644,
      "source_latg": 19.0176147,
      "tax": 0
    }
  }
}

这里" payment_time"是付款完成时的时间戳。

我想要的回答是:

{
    "RH0oZ0Ypbkur3wJM3HMvM150147833457957": {
      "customer_id": "aYQFbwLlf4OCRdWhNKO9XCO08xY2",
      "destination_address": "Sarovar Park Plaza Hotels and Resorts Private Limted\nNo 1\nSector 10\nCBD Belapur\nWadala West\nWadala\nMumbai\nMaharashtra 400614\nIndia",
      "destination_lang": 72.8561644,
      "destination_latg": 19.0176147,
      "discount": 0,
      "driver_id": "cIyZQIJ7tsdvF1a9KpRrKucF2o62",
      "drop_time": "",
      "fare": 0,
      "payment_type": 150149034812772,
      "pickup_time": "",
      "priceperkm": 0,
      "ride_confirm_time": "2017-07-31 05:18:54 +0000",
      "source_address": "Smokin Joe's Fresh Pizza\nShop No. 2\n3\nGround Floor\nAbhiman II\nWadala West\nThane West\nMumbai\nMaharashtra 400602\nIndia",
      "source_lang": 72.8561644,
      "source_latg": 19.0176147,
      "tax": 0
    },
    "1trcf0Ypbkur3wJM3HMvM150147833457957": {
      "customer_id": "aYQFbwLlf4OCRdWhNKO9XCO08xY2",
      "destination_address": "Sarovar Park Plaza Hotels and Resorts Private Limted\nNo 1\nSector 10\nCBD Belapur\nWadala West\nWadala\nMumbai\nMaharashtra 400614\nIndia",
      "destination_lang": 72.8561644,
      "destination_latg": 19.0176147,
      "discount": 0,
      "driver_id": "cIyZQIJ7tsdvF1a9KpRrKucF2o62",
      "drop_time": "",
      "fare": 0,
      "payment_type": 150149034812778,
      "pickup_time": "",
      "priceperkm": 0,
      "ride_confirm_time": "2017-07-31 05:18:54 +0000",
      "source_address": "Smokin Joe's Fresh Pizza\nShop No. 2\n3\nGround Floor\nAbhiman II\nWadala West\nThane West\nMumbai\nMaharashtra 400602\nIndia",
      "source_lang": 72.8561644,
      "source_latg": 19.0176147,
      "tax": 0
    } 
}

我想要特定的" customer_id"前10条记录我在查询中传递了所有" payment_time"。我也想做同样的分页。即在第二次查询呼叫中,它必须返回11-20条记录,依此类推。

2 个答案:

答案 0 :(得分:2)

问题和评论有一些不同的标准,但让我在高层面上解决它;

第一个答案是:Firebase无法查询一个孩子的价值,然后由另一个孩子订购。

简单查询函数表示:

let query = ridesRef.queryOrdered(byChild: "cust_id").queryEqual(toValue: "cust id 4")

要完成该任务,请查询所需的子数据,在本例中为所有客户ID 4个节点,然后在代码中进行排序。这是一个例子

class RideClass {
    var key = ""
    var cust_id = ""
    var pay_time = ""

    init(key: String, cust_id: String, pay_time: String) {
        self.key = key
        self.cust_id = cust_id
        self.pay_time = pay_time
    }
}

var rideArray = [RideClass]()

func populateRideArray() {
    let usersRef = self.ref.child("ride_details")
    let query = usersRef.queryOrdered(byChild: "cust_id").queryEqual(toValue: "cust id 4") 
    query.observeSingleEvent(of: .value, with: { snapshot in 
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let dict = snap.value as! [String: Any]
            let key = snap.key
            let custId = dict["cust_id"] as! String
            let payTime = dict["pay_time"] as! String
            let ride = RideClass(key: key, cust_id: custId, pay_time: payTime)
            self.rideArray.append(ride)
        }

        for ride in self.rideArray {  //unsorted example
            print(ride.pay_time)
        }

        self.rideArray.sort { $0.pay_time < $1.pay_time } //sort

        for ride in self.rideArray {  //sorted example
            print(ride.pay_time)
        }
    })
}

在这个例子中,我们创建了一个RideClass,它存储了一些关于骑行的信息,然后是一个可以用作tableView dataSource的游乐设施数组。

然后查询所有用于cust id 4的游乐设施。我们有一个循环来显示未被解开的内容,然后是这个小宝石

self.rideArray.sort { $0.pay_time < $1.pay_time }

通过pay_time对乘坐阵列进行排序,这可以回答问题。

假设有100,000个骑乘子节点。加载所有数据并在代码中进行排序可能会对内存产生挑战。你做什么的?

我们利用复合值;与cust_id和pay_time的子节点一起,我们还包括id_time。这是一个可能的结构:

  "ride_details" : {
    "ride_0" : {
      "cust_id" : "cust id 4",
      "id_time" : "cust id 4_172200",
      "pay_time" : "172200"
    },
    "ride_1" : {
      "cust_id" : "cust id 2",
      "id_time" : "cust id 2_165500",
      "pay_time" : "165500"
    },
    "ride_2" : {
      "cust_id" : "cust id 1",
      "id_time" : "cust id 1_182300",
      "pay_time" : "182300"
    },
    "ride_3" : {
      "cust_id" : "cust id 3",
      "id_time" : "cust id 3_131800",
      "pay_time" : "131800"
    },
    "ride_4" : {
      "cust_id" : "cust id 4",
      "id_time" : "cust id 4_132200",
      "pay_time" : "132200"
    }
  },

然后以正确的顺序在cust id 4节点中读取一些代码

    let ridesRef = self.ref.child("ride_details")
    let query = ridesRef.queryOrdered(byChild: "id_time")
                        .queryStarting(atValue: "cust id 4_")
                        .queryEnding(atValue: "cust id 4_\\uf8ff")
    query.observeSingleEvent(of: .value, with: { snapshot in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let dict = snap.value as! [String: Any]
            let key = snap.key
            let custId = dict["cust_id"] as! String
            let payTime = dict["pay_time"] as! String
            let ride = RideClass(key: key, cust_id: custId, pay_time: payTime)
            self.rideArray.append(ride)
        }

        for ride in self.rideArray {  //unsorted example
            print(ride.pay_time)
        }
    })

有两点需要注意:

必须迭代快照以维护子序列

&#34; \ uf8ff&#34;是Unicode中代码级别非常高的字符 - 因为它包含所有前面的字符。

答案 1 :(得分:0)

有两种基本方法可以满足您的要求:

  1. 构建一个按payment_time排序的查询,限制只包含前10条记录,然后保存对第10条记录的引用,以便您可以使用queryStartingAtValue进行后续分页调用过滤

  2. 设置一个firebase云功能,数据库触发器监听payment_time节点,这样每次使用数据库中的值更新空的sting时,您都会转换数据,以便将其组织为这种方式使得满足您的需求变得微不足道。例如 - 我会在一个如下所示的路径中组织新数据:customer_ride_details/{{customer_id}}/{{ride_id}}。而且,当您使用时间戳替换payment_time空字符串时触发该函数。钥匙应该已经订购,供您消费。您仍然需要像使用选项1那样管理您的分页。