"how to apply multiple filter in firebase query in swift?" Code Answer

5

the question and comments have some different criteria but let me address it at a high level;

the first answer is: firebase cannot be queried for the value of one child and then ordered by another.

the simple query function expresses that:

let query = ridesref.queryordered(bychild: "cust_id").queryequal(tovalue: "cust id 4")

to accomplish that task, query for the child data you want, in this case all customer id 4 nodes, and then order in code. here's an example

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)
        }
    })
}

in this example, we create a rideclass which stores some info about the ride, and then an array of rides which could be used as a tableview datasource.

then query for all rides that are for cust id 4. we have a loop to show what was retreived unsorted and then this little gem

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

which sorts the ride array in place by pay_time, which answers the question.

suppose though, there are 100,000 ride child nodes. loading in all of that data and sorting in code could be challenging memory wise. what do you do?

we leverage a compound value; along with the child nodes of cust_id and pay_time, we also include id_time. here's a possible structure:

  "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"
    }
  },

and then some code to read in the cust id 4 nodes in the correct order

    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)
        }
    })

two things to note:

the snapshot must be iterated over to maintain the child sequence

"uf8ff" is a character at a very high code level in unicode - because of that it encompasses all of the preceeding characters.

By Steve Costello on May 18 2022

Answers related to “how to apply multiple filter in firebase query in swift?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.