如何使用Firebase服务器时间戳生成日期? [英] How to use the Firebase server timestamp to generate date created?
问题描述
目前,Google的版本 ServerValue.TIMESTAMP
会返回 {。sv:timestamp}
被用作Firebase的指令,一旦您将数据保存到Firebase服务器,就会使用服务器时间戳填充该字段。
当您在客户端创建数据,你还没有实际的时间戳玩(即使用作为创建日期)。在初始保存和随后的检索之后,您只能访问时间戳,我想 - 这有时为时已晚,不够高雅。
在Google之前: b 更新:忽略此部分,因为它不正确 - 我误解了这些示例。 据我所知,在Firebase之前版本中似乎有一个服务器生成的时间戳,可以让您获取实际的时间戳: 问题: >在客户端,因为我一直在阅读这是不安全的,但请分享您的想法,如果你认为不同。 您在写入操作中使用 假设我们运行这段代码: 这将执行如下: 自Firebase加入Google以来,我们生成 Currently, the Google's version of When you create your data on the client side however, you don't have the actual timestamp to play with yet (ie. use as the creation date). You only will have an access to the timestamp after the initial save and consequent retrieval, which - I imagine - is sometimes too late and not very elegant. Before Google: Update: Ignore this section as it is incorrect - I misunderstood the examples. As far as I understand in pre-google Firebase there seemed to be a server-generated timestamp available that allowed you to acquire the actual timestamp: Questions:
Note: I'm not considering using When you use the Let's say we run this code: This will execute as follows: If you're using Nothing changed in the way we generate the 这篇关于如何使用Firebase服务器时间戳生成日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
ServerValue.TIMESTAMP
总是返回 {。sv:timestamp}
。
import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP //例如1466094046
$我不考虑使用 new Date()
$ b ServerValue.TIMESTAMP
常量,则表示Firebase数据库服务器应在执行写入操作时确定正确的时间戳。
$ ref $ add $ value $ {
public void onDataChange(DataSnapshot dataSnapshot){
System.out.println(dataSnapshot.getValue());
}
public void onCancelled(DatabaseError databaseError) {}
});
ref.setValue(ServerValue.TIMESTAMP);
ServerValue.TIMESTAMP
ChildEventListener
而不是<$ c
$ c> ValueEventListener ,那么客户端将在步骤3中调用 onChildAdded
,并在 onChildChanged
中调用第8步。
ServerValue.TIMESTAMP
的方式没有任何变化。以前工作的代码将继续工作。这也意味着第一回答你的链接是一个有效的方式来处理它。ServerValue.TIMESTAMP
returns {".sv":"timestamp"}
which is used as a directive for Firebase to fill that field with the server timestamp once you save the data to the Firebase server.
ServerValue.TIMESTAMP
always returned the {".sv":"timestamp"}
.import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP // eg. 1466094046
new Date()
on client side as I've been reading it's not safe, though please share your thoughts if you think different.ServerValue.TIMESTAMP
constant in a write operation, you're saying that the Firebase Database server should determine the correct timestamp when it executes the write operation.ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot.getValue());
}
public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);
ServerValue.TIMESTAMP
ChildEventListener
instead of a ValueEventListener
, then the client will call onChildAdded
in step 3 and onChildChanged
in step 8.ServerValue.TIMESTAMP
since Firebase joined Google. Code that worked before, will continue to work. That also means that the first answer you linked is a valid way to handle it.