How can I get Timestamp from Firestore server without bothering for the case when device clock is out of sync with Firestore

微笑、不失礼 提交于 2020-02-23 06:26:27

问题


This is in reference to this question Firebase Timestamp difference between current Timestamp and database timestamp error

If I use Timestamp.now() there are cases where the device clock is out of sync with Firestore and my time calculations are not accurate. I want to get the exact time from the Firestore server without being bothered by out of sync cases. Is there any way I could do that?

I have seen FieldValue.serverTimestamp() but there I cannot find a way to calculate anything using it, since it's return type is FieldValue and I am unable to parse it to Timestamp.


回答1:


If you are using FieldValue.serverTimestamp() it means you let Firestore generate a timestamp for you. Please note that this timestamp is generated entirely on the server. It is not generated as document ids are, for instance on the client.

I cannot find a way to calculate anything using it since it's return type is FieldValue and I am unable to parse it to Timestamp.

When calling FieldValue's serverTimestamp() method, it true that type of the object that is returned is FieldValue but this only used to be passed as an argument when you need that timestamp property. Please check how you can add a timestamp to a Cloud Firestore document:

  • ServerTimestamp is always null on Firebase Firestore

So if you query the database and you get a document that has a property that was set using FieldValue.serverTimestamp(), the type of object that is returned is Date. Since you get a Date object back, you can do whatever you want with it.

However, in Firebase Realtime Database we have another mechanism. So please see this answer:

  • How to save the current date/time when I add new value to Firebase Realtime Database

So, in this case, it's totally different since we are using the time since the Unix epoch, in milliseconds and not a Date object.

Edit:

It sounds like your question started off about how to get a Timestamp from Firestore, but is actually just about how to structure a document so two devices can read the same document. However, to make it work, you should use two properties of type Date. One for device A and one for device B. Check the schema below:

Firestore-root
   |
   --- yourCollection (collection)
        |
        --- yourDocument (document)
              |
              --- deviceA: January 24, 2020 at 3:37:59 PM UTC+3 
              |
              --- deviceB: January 25, 2020 at 3:37:59 PM UTC+3 //Timestamp

To get the values of deviceA and deviceB properties, please use my answer from the following post:

  • How to get server Timestamp from Firestore in an android device?

Now you can compare both timestamps.




回答2:


One obvious but economically adverse way is to write to Firestore Database, the timestamp and read it again from there.

db.collection("TimestampChecker").document("some_string").set(new TimestampGetter()).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

            db.collection("TimestampChecker").document("some_string").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if(task.getResult()!=null)
                    {
                        TimestampGetter timestampGetter = task.getResult().toObject(TimestampGetter.class);
                        Date dateNow = timestampGetter.getTimestampNow();

                    }

                }
            });

        }
    });

And TimestampGetter can be a class like this:

public class TimestampGetter {
@ServerTimestamp
private Date timestampNow;

public TimestampGetter(Date timestampNow) {
    this.timestampNow = timestampNow;
}

public TimestampGetter() {
}

public Date getTimestampNow() {
    return timestampNow;
}

public void setTimestampNow(Date timestampNow) {
    this.timestampNow = timestampNow;
}}


来源:https://stackoverflow.com/questions/59908304/how-can-i-get-timestamp-from-firestore-server-without-bothering-for-the-case-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!