如何使用服务器时间在Firestore中设置时期 [英] How to set epoch in Firestore using server time

查看:180
本文介绍了如何使用服务器时间在Firestore中设置时期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Firestore中创建数据时设置新纪元。我正在使用 ServerValue.TIMESTAMP 来获得与实时数据库中类似的结果。

我不想通过使用设备时间 System.getCurrentMillis 来设置它,因为用户可以更改这个时间。 / p>

根据



我很惊讶这是西班牙语,在某些情况下可能有用,是我追逐的。尝试看到光明的一面,坚持下去,并认为我看到西班牙语的网页,所以我改变了页脚选择器的语言,它没有改变。在这一点上,我假设是在项目语言设置。



回到时代的尝试。考虑到我的项目也使用实时数据库,我试图设置它:

  Map< String,对象> map = new HashMap<>(); 
map.put(timestamp,ServerValue.TIMESTAMP);
reference.update(map);

它确实上传了一些内容,但这只是无稽之谈。



java.util.Date 的一个实例。当您稍后阅读该值时,可以使用获取历元时间例如,对于像这样创建的文档:

<$ p $ getTime()

p> 映射< String,Object> doc = new HashMap<>();
doc.put(timestamp,FieldValue.serverTimestamp());

结果值可以像这样读取:

<@ p $ p> docRef.get()。addOnCompleteListener(new OnCompleteListener< DocumentSnapshot>(){
@Override
public void onComplete(@NonNull Task< DocumentSnapshot> {
if(task.isSuccessful()){
DocumentSnapshot snapshot = task.getResult(); $ b $ if(snapshot!= null){
Map< String,Object> map = snapshot.getData();
Date date =(Date)map.get(timestamp);
Log.d(TAG,date =+ date);
Log。 d(TAG,time =+ date.getTime());
} else {
Log.d(TAG,No such document);
}
} else {
Log.d(TAG,get()failed with,task.getException());
}
}
});


I'm trying to set the epoch when data is created in Firestore. I'm looking to get some similar result to what is done in the real-time database, using ServerValue.TIMESTAMP.

I don't want to set it by using the device time System.getCurrentMillis because that time can be changed by the user.

According to docs an update needs to be done, the problem with that is the format. This is my code:

Map<String, Object> map = new HashMap<>();
map.put("timestamp", FieldValue.serverTimestamp());
reference.update(map);

And this is the result in the Firebase web console:

I was very surprised it is in spanish, which could be useful in some situations but epoch is what I'm chasing. Try to see the bright side and stick with it and thought that I was seeing the web in spanish, so I changed the language in the footer selector, it didn't change. On this point I'm assuming is set in the project language.

Back to the epoch attempt. Considering my project is using the real-time database as well, I try to set it in that way:

Map<String, Object> map = new HashMap<>();
map.put("timestamp", ServerValue.TIMESTAMP);
reference.update(map);

It did upload something, but it was just nonsense.

I think using epoch as the server-side timestamp is a better standard approach, after that every client can transform it to the user convenience and locale.

Can the epoch by set as server value in Firestore?

UPDATE

The answer marked as correct lead me to some interesting findings that I would like to share, so others in the same situation can benefit from:

  • There is no need to set the epoch because of the FieldValue.serverTimestamp() it is a date object handled by the database, what we see in the console is just a friendly way to show it.
  • Since FieldValue.serverTimestamp() is a date object it can be ordered as any other timestamp could be, if you add orderBy("timestamp", Query.Direction.DESCENDING) to your query (or Query.Direction.ASCENDING) it will sort the results correctly.
  • And regarding to a the @34m0 comment, that is right, clients should not take care of the logic for setting the creation or update time, but it should be done in Functions, specially considering an update need to be done in the client to set the value. Opposite to how is done in the real-time database where it can be set directly when the write is executed, in Firestore it could be done by using a Map for the entire object, but that break the POJO purpose.

解决方案

The object that results from setting a Firestore field with FieldValue.serverTimestamp() is an instance of java.util.Date. When you later read the value, you can get the epoch time using getTime().

As an example, for a document created like this:

Map<String, Object> doc = new HashMap<>();
doc.put("timestamp", FieldValue.serverTimestamp());

The resulting value can be read like this:

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot snapshot = task.getResult();
            if (snapshot != null) {
                Map<String,Object> map = snapshot.getData();
                Date date = (Date) map.get("timestamp");
                Log.d(TAG, "date=" + date);
                Log.d(TAG, "time=" + date.getTime());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get() failed with ", task.getException());
        }
    }
});

这篇关于如何使用服务器时间在Firestore中设置时期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆