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

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

问题描述

目前,Google的版本 ServerValue.TIMESTAMP 会返回 {。sv:timestamp} 被用作Firebase的指令,一旦您将数据保存到Firebase服务器,就会使用服务器时间戳填充该字段。



当您在客户端创建数据,你还没有实际的时间戳玩(即使用作为创建日期)。在初始保存和随后的检索之后,您只能访问时间戳,我想 - 这有时为时已晚,不够高雅。




在Google之前: b

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



据我所知,在Firebase之前版本中似乎有一个服务器生成的时间戳,可以让您获取实际的时间戳:

  import com.firebase.client.ServerValue; 
ServerValue.TIMESTAMP //例如1466094046

ref 1 ref 2




问题:


  1. 这样的保存/检索是唯一的方法在我的模型实例上获取服务器生成的创建日期?
  2. 如果是,您是否可以提出实现这种模式的方法?

  3. 我是否理解ServerValue.TIMESTAMP随着Google收购Firebase而改变了吗?更新:不,@FrankvanPuffelen回复说在收购过程中没有任何变化。





  4. $我不考虑使用 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);

    这将执行如下:


    1. 附加一个侦听器

    2. 您使用 ServerValue.TIMESTAMP

    3. Firebase客户端立即触发一个值事件,并将其写入服务器的时间戳近似值
    4. 您的代码打印该值

    5. 写操作发送到Firebase服务器

    6. Firebase服务器确定实际时间戳并将值写入数据库(假设没有安全规则失败)
    7. Firebase服务器将实际时间戳记发送回客户端

    8. Firebase客户端为实际值引发值事件 您的代码打印如果您使用 ChildEventListener 而不是<$ c
    9. ,那么
      $ c> ValueEventListener ,那么客户端将在步骤3中调用 onChildAdded ,并在 onChildChanged 中调用第8步。

      自Firebase加入Google以来,我们生成 ServerValue.TIMESTAMP 的方式没有任何变化。以前工作的代码将继续工作。这也意味着第一回答你的链接是一个有效的方式来处理它。


      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.


      Before Google:

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

      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
      

      (ref 1, ref 2)


      Questions:

      1. Is such save/retrieval the only way to get the server-generated creation date on my model instances?
      2. If yes can you propose a method of implementing such pattern?
      3. Am I understanding correctly ServerValue.TIMESTAMP has changed with Google's acquisition of Firebase? Update: No, @FrankvanPuffelen replied that nothing's changed during acquisition.


      Note:

      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.

      解决方案

      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);
      

      This will execute as follows:

      1. you attach a listener
      2. you write a value with ServerValue.TIMESTAMP
      3. the Firebase client immediate fires a value event with an approximation of the timestamp it will write on the server
      4. your code prints that value
      5. the write operation gets sent to the Firebase servers
      6. the Firebase servers determine the actual timestamp and write the value to the database (assuming no security rules fail)
      7. the Firebase server send the actual timestamp back to the client
      8. the Firebase client raises a value event for the actual value
      9. your code prints that value

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

      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天全站免登陆