Firebase UIDs as a time indicator doesn't seem to be consistent...

393 views
Skip to first unread message

Devan Sabaratnam

unread,
Jan 15, 2015, 7:27:57 AM1/15/15
to fireba...@googlegroups.com
Forgive me if this is a double post - but I was sure I posted this question earlier today, but cannot find it in Google Groups now.

My question is:  I read that the unique UIDs that Firebase generates during a push() is based on the server timestamp, and thus can be used as a sort field to arrange your data in chronological order during a query.

It seemed to work in this fashion for the first few days of my testing, but just today during testing, I entered a new object into Firebase, and noticed that it was 'sorted' in the middle of the existing list when I did an orderBy:$id in Angular/Ionic.  I reversed the order but still noticed that the new key was in the middle of the dataset.  Checking the UID also showed that yes, the list was being sorted correctly and that the UID would NOT have sorted at the end of the list as expected.

Just wondering if this was a rare anomaly?  CAN we in fact trust the Firebase generated UIDs to always be ordered sequentially in the order they are created?  My app stores comments and threads in Firebase, so ideally they would need to be in chronological order.  No biggie if it doesn't, as I can create a date/time key and use that as the orderBy parameter, but I was curious about this issue.

Cheers,
Devan

Jacob Wenger

unread,
Jan 15, 2015, 5:25:17 PM1/15/15
to fireba...@googlegroups.com
Hey Devan,

Firebase push IDs are 100% guaranteed to sort in chronological order (if they don't, it's a bug). If you specify a .priority or do an orderByChild of some child key, then your results will get returned in a different order though. I'm not sure what "I did an orderBy:$id in Angular/Ionic" means, but that is probably where your sort got mixed up.

Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/3bc09afa-d731-4b20-bae4-cc47ed9acb37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Lehenbauer

unread,
Jan 15, 2015, 5:35:26 PM1/15/15
to fireba...@googlegroups.com
Hey Devan,

Jacob is mostly right. :-)

push IDs are generated client-side and are therefore at the mercy of the client's local clock (which could be completely wrong).  We actually try to compensate for wrong clocks to some degree though.  When a Firebase client connects, the server will send an accurate timestamp to the client so it can then use that as a basis to adjust its own clock to be more correct when generating push ID's.

But note that this correction only takes effect once the client has connected to Firebase, so if you call .push() before then you may get non-chronological IDs.  In fact, you can even call .push() twice in a row on the same client and the second push ID will be ordered before the first one (if we started compensating your clock between the two push() calls).

Anyway, long story short: push() usually works out to be chronological, but it's definitely not guaranteed.  To get guaranteed choronological order, you can do something like:

ref.push({ foo: 'bar', date: Firebase.ServerValue.TIMESTAMP });

// later, retrieve them ordered by date.
ref.orderBy('date').on('child_added', ...)

This is guaranteed to give you chronological ordering, since the ServerValue.TIMESTAMP will be resolved on the Firebase server to be an accurate timestamp.

Hope this helps,
-Michael

Devan Sabaratnam

unread,
Jan 15, 2015, 7:34:26 PM1/15/15
to fireba...@googlegroups.com

Jacob and Michael,

Thanks for the responses and the clarification.  I understand things a bit better now.  The fact that the UID gets generated client side is enlightening, and probably a factor here as I entered the original test data on one device and then the outlier object that would not sort was entered on a totally different device.  There could have been a difference in locale or local time on that device, I am thinking.

We will look in future at using Firebase.ServerValue.TIMESTAMP as the datum for the time based sorting field.

Thank You,
Devan



Reply all
Reply to author
Forward
0 new messages