如何使用 Firebase 服务器时间戳生成创建日期? [英] How to use the Firebase server timestamp to generate date created?

查看:34
本文介绍了如何使用 Firebase 服务器时间戳生成创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,ServerValue.TIMESTAMP 的 Google 版本返回 {".sv":"timestamp"} 用作 Firebase 的指令来填充该字段将数据保存到 Firebase 服务器后的服务器时间戳.

Currently, the Google's version of 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.

然而,当您在客户端创建数据时,您还没有可使用的实际时间戳(即用作创建日期).您只能在初始保存和随后的检索之后访问时间戳,我想这有时为时已晚,而且不是很优雅.

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.

在 Google 之前:

更新:忽略此部分,因为它不正确 - 我误解了示例.ServerValue.TIMESTAMP 总是返回 {".sv":"timestamp"}.

Update: Ignore this section as it is incorrect - I misunderstood the examples. ServerValue.TIMESTAMP always returned the {".sv":"timestamp"}.

据我了解,在 Google Firebase 之前,似乎有一个服务器生成的时间戳可用,可让您获取实际时间戳:

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:

import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP // eg. 1466094046

(参考 1, 参考 2)

问题:

  1. 这种保存/检索是在我的模型实例上获取服务器生成的创建日期的唯一方法吗?
  2. 如果是,你能提出一种实现这种模式的方法吗?
  3. 我是否正确理解 ServerValue.TIMESTAMP 是否随着 Google 收购 Firebase 发生了变化?更新:不,@FrankvanPuffelen 回答说在收购过程中没有任何变化.

<小时>

注意:

我不考虑在客户端使用 new Date(),因为我一直在阅读它不安全,但如果您有不同的想法,请分享您的想法.

I'm not considering using 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 常量时,您是说 Firebase 数据库服务器应该确定正确的执行写操作时的时间戳.

When you use the 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.

假设我们运行这段代码:

Let's say we run this code:

ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue()); 
    }

    public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);

这将执行如下:

  1. 你附加了一个监听器
  2. 你用 ServerValue.TIMESTAMP
  3. 写一个值
  4. Firebase 客户端立即触发一个 value 事件,其时间戳近似于它将写入服务器
  5. 您的代码打印该值
  6. 写入操作被发送到 Firebase 服务器
  7. Firebase 服务器确定实际时间戳并将值写入数据库(假设没有安全规则失败)
  8. Firebase 服务器将实际时间戳发送回客户端
  9. Firebase 客户端为实际值引发一个值事件
  10. 您的代码打印该值

如果您使用 ChildEventListener 而不是 ValueEventListener,则客户端将在步骤 3 中调用 onChildAddedonChildChanged 在第 8 步.

If you're using ChildEventListener instead of a ValueEventListener, then the client will call onChildAdded in step 3 and onChildChanged in step 8.

自从 Firebase 加入 Google 以来,我们生成 ServerValue.TIMESTAMP 的方式没有任何改变.以前有效的代码将继续有效.这也意味着 首先您链接的答案是处理它的有效方法.

Nothing changed in the way we generate the 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.

这篇关于如何使用 Firebase 服务器时间戳生成创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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