Reputation: 5742
Firebase Realtime Database uses a push ID with a chronological prefix, thus new objects are always sorted by its creation timestamp.
Given offline capabilities is enable on Android and iOS app, if the user has disabled the automatic clock synchronization, will the SDK:
In 2019 Brazilian government canceled Daylight Saving Time without giving the proper time to carriers and internet providers get prepared to it. Consequently clocks were automatically advanced, crashing several distributed systems in Brazil due timestamp inconsistency. This phenomena is popular known as Bolsonaro's Bug.
Desperate because they were no longer receiving orders from customers, they turned off the automatic clock adjustment and set it manually, users who depends on their mobile devices for work (ex. taxi drivers, deliverer professionals, etc) turned off the automatic clock update on their devices and manually turned their block back in 1 hour. Thus:
The scenario is problematic when the user save data offline (with a bad clock) and hours later tries to sync with the server.
Since Firebase Realtime Database has offline capabilities and push IDs are chronologically sorted, in this very specific scenario, would it be possible to:
Upvotes: 1
Views: 190
Reputation: 317712
An inaccurate local clock could cause inaccurate push IDs, in terms of time ordering. If you need accurate times for the purpose of sorting, I wouldn't rely too heavily on the ordering of push IDs.
You might want to read the documentation on clock skew for more information on what you can do to deal with possible time differences:
While firebase.database.ServerValue.TIMESTAMP is much more accurate, and preferable for most read/write operations, it can occasionally be useful to estimate the client's clock skew with respect to the Firebase Realtime Database's servers. You can attach a callback to the location /.info/serverTimeOffset to obtain the value, in milliseconds, that Firebase Realtime Database clients add to the local reported time (epoch time in milliseconds) to estimate the server time. Note that this offset's accuracy can be affected by networking latency, and so is useful primarily for discovering large (> 1 second) discrepancies in clock time.
DatabaseReference offsetRef = FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset"); offsetRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { double offset = snapshot.getValue(Double.class); double estimatedServerTimeMs = System.currentTimeMillis() + offset; } @Override public void onCancelled(@NonNull DatabaseError error) { Log.w(TAG, "Listener was cancelled"); } });
Upvotes: 2