Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving dates in Firebase Database

Sorting some data based on dates in my Android application. It is stored as follows:

Map<Year, Map<Month, Map<Day, Stuff>>>

Year, month and day are simply containing integers. This cannot be saved to Firebase database since it is a map that has a non-string key. I though about flattening this and turning it into Map. Huge compromise on the data structure since it allows data that the previous one does not(and should not) allow. However, Date isn't either a String. I tried something like this:

String key = String.valueOf(date.getTime());

And then using a data structure:

Map<String, Stuff>

Doesn't work either, it seems to use som illegal characters that the String cannot have in Firebase database. So, how would I go about making this work? Would appreciate help.

like image 646
why_vincent Avatar asked Jan 05 '23 08:01

why_vincent


1 Answers

I would advise you to create a structure that would look like this:

...
date:{
   day:1,
   month:04,
   year:2016,
   timestamp:12345679979 //in milliseconds
}

This way you can easily index the timestamp int the security rule, do a query by Child and use the filter using start At and endAt methods where you would define the timestamps that define the ranges

like image 141
realdm Avatar answered Jan 14 '23 07:01

realdm