使用 Firebase 时钟为更改添加时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13036994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Firebase clock to timestamp changes
提问by Kartik
We have a dataset in Firebase updated by multiple clients. We want to track the last modified on datetime of the dataset. We cannot rely on the client setting the modified on datetime as their local clock can be totally out of sync.
我们在 Firebase 中有一个由多个客户端更新的数据集。我们要跟踪数据集的日期时间上次修改。我们不能依赖客户端设置修改日期时间,因为他们的本地时钟可能完全不同步。
Is there a way I could have Firebase tag timestamp to a dataset based on its clock to track last modified on?
有没有办法可以根据数据集的时钟将 Firebase 标记时间戳添加到数据集以跟踪上次修改时间?
采纳答案by Andrew Lee
We're working on some features to support this at the moment, but we don't have a way to do this right now. Note, however, that the IDs created by "push()" are chronologically-ordered, and we compensate for client-side clock skew as best we can when we create them, so if all you're trying to do is make sure some writes to a list occur in order, you can do that with push().
我们目前正在开发一些功能来支持这一点,但我们现在没有办法做到这一点。但是请注意,“push()”创建的 ID 是按时间顺序排列的,我们在创建它们时尽可能地补偿客户端时钟偏差,所以如果您要做的只是确保一些写入列表按顺序发生,您可以使用 push() 来做到这一点。
I'd be interested in hearing how you'd like this feature to look. If you have a sec I'd appreciate an email to andrew at firebase...
我很想知道您希望此功能的外观如何。如果你有时间,我会很感激在 firebase 给安德鲁发一封电子邮件......
Update:Firebase now supports setting server timestamps as well as accessing the server time directly on the client. See the documentation here: https://www.firebase.com/docs/managing-presence.html
更新:Firebase 现在支持设置服务器时间戳以及直接在客户端访问服务器时间。请参阅此处的文档:https: //www.firebase.com/docs/managing-presence.html
回答by oori
We're on the same boat... and I guess sooner or later, many firebase clients will have this issue, as it's a "new kind" of problem we never had to face in the old "client-server" days.
我们在同一条船上……我想迟早会有许多 Firebase 客户端遇到这个问题,因为这是我们在旧的“客户端-服务器”时代从未遇到过的“新型”问题。
Out current solution (still in the making) is to estimate the time difference between the client and OUR server on initialization, and them compensate,
So - where we had x = new Date(),
it is now x = ourDateService.now()
当前的解决方案(仍在制定中)是在初始化时估计客户端和我们的服务器之间的时间差,并进行补偿,
所以 - 我们有x = new Date(),
现在是x = ourDateService.now()
our now() function simply does new Date() + diff
我们的 now() 函数只是执行new Date() + diff
This works for us,as we don't need millisecond accuracy, and it's a single-page-app that loads once, so we can do this diff-check and initialize ourDateService on load, but this solution will not work for everybody (ofcourse, you can store it in the localstorage/cookie and revalidate every day or so).
这适用于我们,因为我们不需要毫秒的准确性,这是一个单页的应用程序加载一次,所以我们可以做到这一点差异检查和负载初始化ourDateService,但这种方法会为大家不要工作(ofcourse ,您可以将其存储在 localstorage/cookie 中并每天重新验证一次)。

