Firestore时间戳记为空 [英] Firestore timestamp getting null
问题描述
我正在android项目中使用firestore,一切正常.但是昨天从文档快照获取时间戳值时出现了问题.
I'm using firestore in my android project, everything working fine.But Yesterday an issue occurred on getting timestamp value from document snapshot.
if (documentChange.getType() == DocumentChange.Type.ADDED) {
Map<String, Object> stringObjectMap = documentChange.getDocument().getData();
Date date = (Date) stringObjectMap.get("timestamp"); }
这是我的收藏文件之一.
This is one of my Collection Document.
id : "25",
message : "This is my message",
timestamp : December 11, 2017 at 10:39:12 PM UTC+5, \\This is Firebase FieldValue.serverTimestamp()
username : "Temp"
我从文档中获得了除时间戳之外的所有内容.
I get everything from the document except for timestamp.
推荐答案
如果从同一客户端写入后看到带有空时间戳记的ADDED
事件,则这是预期的行为.
If you are seeing this ADDED
event with a null timestamp after writing from the same client, this is the expected behavior.
使用服务器时间戳书写时,您将获得两个事件:
When you write with server timestamps you will get two events:
- 一个初始事件,用于确认文档在本地缓存中的写入.
此时时间戳为
null
,因为服务器尚未设置 还没. - 第二个事件,其中服务器已经设置了时间戳,并且 客户已观察到更改.
- An initial event confirming the document write in the local cache.
At this point the timestamp is
null
because the server has not set it yet. - A second event where the timestamp has been set by the server and the client has observed the change.
我们正在开发一个API,以使此行为可配置,但是现在您需要处理这种情况.
We are working on an API to make this behavior configurable, but right now you need to handle this case.
编辑,您现在可以控制此行为.
Edit You can now control this behavior.
DocumentSnapshot snap = ...
// One of ESTIMATE, NONE, or PREVIOUS
DocumentSnapshot.ServerTimestampBehavior behavior = ...
Date date = snap.getDate("fieldName", behavior);
以前的行为(未知时返回null
)等同于新API中的NONE
.有关更多信息,请参见JavaDoc:
https://firebase.google.com/docs /reference/android/com/google/firebase/firestore/DocumentSnapshot.ServerTimestampBehavior
The previous behavior (returning null
when not known) is equivalent to NONE
in the new API. For more information, see the JavaDoc:
https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot.ServerTimestampBehavior
这篇关于Firestore时间戳记为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!