Home > Net >  Firebase query for timestamp in subkey not working
Firebase query for timestamp in subkey not working

Time:09-25

I have the following code:

DatabaseReference ref = lib.getFirebaseDatabase().getReference("data/newquestions");
        Query q = ref.orderByChild("date/timeUTC");
        long t = dtStart.getTimeUTC();
        q.startAt(t);
        q.addListenerForSingleValueEvent(new ValueEventListener() {

This is sample data:

  {
  "date" : {
    "time" : 1632050827132,
    "timeUTC" : 1632043627132,
    "timezone" : "Europe/Berlin"
  },
  "enddate" : {
    "time" : 1632137227132,
    "timeUTC" : 1632130027132,
    "timezone" : "Europe/Berlin"
  },
  "enumcontacttypes" : [ "Public" ],
  "id" : 117,
  "public" : true,
  "userid" : 8
}

I always get the same number of values independent of what date I specify. The startAt() function has no effect.

CodePudding user response:

Since I cannot see a more detailed database schema, I cannot say if the intended query will work as expected. What I can say, is that the Firebase Realtime Database queries are immutable. This means that you cannot change the properties of an existing query. If you change the value by calling the .startAt(t) method, then it becomes a new query. So the simples solution would be to chain all method calls in a single Query object like this:

DatabaseReference ref = lib.getFirebaseDatabase().getReference("data/newquestions");
long t = dtStart.getTimeUTC();
Query q = ref.orderByChild("date/timeUTC").startAt(t); //Added to the initial query
q.addListenerForSingleValueEvent(/* ... /*);
  • Related