Realtime DB Order of Entry

Hello Thunkers,
I have question , when we create new entry in realtime db it has been observed that , the order of entry is not sequential.

Is this expected behavior, or there is some bug in my code.
Can we do anything to keep dates in sequential manner ??

I am using cloud variable to add entry into realtime DB.

I am preparing graphs which would be taking data from realtime db, hence i want them sequential.

Please help, Thanks Rohan

1 Like

The “6” property has sub-properties for each date. Those will be alphabetized (sorted) by Firebase. Instead, you need to use a list. So you would insert a value as the last item in the list at “6”.

Yes, this is an expected behavior. RTDB will store the data in an object but might not keep the order it was created with.

You can use the IndexOn property in the Rules to force the order but RTDB only understands dates as unix timestamp so you need to add this field and sort using it.

1 Like

Is it returning th in alphabetic order?

You could use the time since 1970 as the key value

you’d get the keys of the value 6 (6 would be an object that’s returned from firebase)
sort that list ascending
loop over that list and pull the properties from each corresponding key/value pair from the object 6

^^^this is how i deal with time based lists in firebase/thunkable. for example in the following images i would be saving a list of study scores under a user id as a list.


^^^ read data from FB

Screen Shot 2022-06-15 at 10.06.32 AM
^^^ data model for each object saved under a key of “seconds since 1970”


^^^parsing the list from firebase. (i never noticed they were out of order :joy: )


^^^saving a score

2 Likes

The alphabet order will sort the entries as follows:

  • 1^6^2022
  • 14^6^2022
  • 9^6^2022

Which is the wrong order of dates.

Thanks Tatiang, i am inserting as an when they are created. The problem is they are inserted in alphabetic order, i think.

1 Like

I am storing Current and Target weight against these dates and preparing graph for the same. I can sort dates but then i have to make sure associated current weight and target weight does not sort. Looks tricky…

1 Like

They shouldn’t be sorted at all if you are using a list. Unless I’m mistaken but I don’t think Firebase would ever auto-sort a list. Are you sure you are inserting the new values as the last item in the list?

1 Like

I am not using a list, user updates their profile every week. I am making an entry to RTDM based on date it was entered. Entry was done on 1st and 9th , when 14th entry came , RTDM inserted in between of these days based on its sorting Logic.

What i did to fix is i removed ^ when entry is made, so that the date of entry is ordered. And Then i added Date field inside this Object. Ex.

162022
|_Weight
|_Target
|_Date

Since Date is sorted, For loop will run thru these days and insert Date, Weight and Target in the list. And it would be displayed in graph.

1 Like

Right but you could just as easily update a list when the user updates their profile. Perhaps you don’t want to do that but that’s how I’ve set up a photo journal app using Firebase so that entries are chronological.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.