0

I am wondering if it is possible to apply multiple queries for the Firebase database in Android/Kotlin.

I have a firebase database that has time value in it. enter image description here

what I need to get is I want to have sales report hourly based. I get this for 1 hour but, when I try it for another hour I don't get the result. I believe I am doing something wrong here logically.

val rootRef = FirebaseDatabase.getInstance().reference
    val query1 = rootRef.child("Sales").orderByChild("time").startAt("00:00:00").endAt("00:30:00")
    val query2 = rootRef.child("Sales").orderByChild("time").startAt("23:30:00").endAt("00:00:00")

    val valueEventListener = object : ValueEventListener {
        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
                    var timeList = arrayListOf<String>()
                    val time = ds.child("time").getValue(String::class.java)!!

                    var totalList = arrayListOf<Double>()

                    timeList.add(time)
                    println("time List : "+timeList)


                }
            }
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }
    query2.addListenerForSingleValueEvent(valueEventListener)
    query1.addListenerForSingleValueEvent(valueEventListener)

Can anyone help me to understand the logic here? Thanks.

......

Edited part:

Here it is what i have done: I am now saving date in miliseconds format as you suggested. And, my code looks like this :

   `val query1 = rootRef.child("Sales").orderByChild("time").startAt(1577192700000).endAt(1577192701167)
    val valueEventListener = object : ValueEventListener {
        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {

                    var timeList = arrayListOf<Long>()
                    val time = ds.child("time").getValue(Long::class.java)!!


                   // var totalList = arrayListOf<Double>()

                    timeList.add(time)
                    println("time List : "+timeList)


                }
            }
        }`

when I use miliseconds (long value) as startAt() paramater it gives me error. enter image description here

1 Answers1

0

I get this for 1 hour but, when I try it for another hour I don't get the result.

The problem in your code is that you are passing strings to the startAt() and endAt() methods. Strings are ordered lexicographically, meaning that the following query:

val query1 = rootRef.child("Sales").orderByChild("time").startAt("00:00:00").endAt("00:30:00")

Will work since 00:30:00 is after 00:00:00 but the second query:

val query2 = rootRef.child("Sales").orderByChild("time").startAt("23:30:00").endAt("00:00:00")

Will not return any results. To solve this, you should store the time as a timestamp and not as a String. To achieve this, please see my answer from the following post:

In the end, just pass timestamps (long values) to both startAt() and endAt() methods and your problem will be solved.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193