未来的时间戳作为火力点的关键 [英] future timestamp as key in firebase

查看:140
本文介绍了未来的时间戳作为火力点的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为将来的事件创建通知。例如类似于提醒。我在想,我可以使用服务器时间戳+未来时间作为关键,而只弹出时间戳超过当前服务器时间的通知。不过,我需要服务器的时间。
我不能使用客户端时间戳,因为它可能不同步。



有没有办法从firebase获取当前时间戳?



我知道有一个占位符对于在服务器端替换的时间戳。我可以使用这个作为一个关键?
我可以想象,当占位符被实时时间戳取代时,有没有办法监听这个事件?



或者通常还有其他的这个问题是否合理?

解决方案

您可能正在解决 X / Y问题在这里,你可能会更好地解释你的用例,并得到一个更好的整体解决方案;获取服务器时间戳在客户端上使用看起来可能是一个概念性问题。



例如,如果我的目标只是在消息中显示时间,我可以简单的调用集,监控路径,并显示它返回的内容:

  var ref = new Firebase(URL); 
ref.on('child_added',function(snap){
//客户端只听路径,记录在到达时提供时间戳
console.log('最后一个事件被添加at',snap.val()。time);
});
ref.push({name:'Kato',time:Firebase.ServerValue.TIMESTAMP});

如果您排队未来的事件,只需将它们存储在不同的路径中,并将它们移动到present 准备好的路径。

如果您设置获取时间戳,最简单的方法是设置一个虚拟路径并设置它的价值:

$ p $ 新的Firebase(URL).transaction(函数(currValue){
返回Firebase.ServerValue .TIMESTAMP;
},函数(err,success,snap){
console.log('当前服务器时间戳',snap.val());
});


I need to create the notifications for future events. for example similar to reminders. I was thinking that I can use the the server timestamp + future time as the key and than pop only the notifications with the timestamp older than current server time. However I need the server time. I can't use client side time stamp as it might be out of sync.

Is there a way to get the current time stamp from firebase?

I know that there is a placeholder for timestamp which is replaced on the server side. Can I use this as a key? I can imagine that probably not so is there a way to listen to this event when the placeholder is replaced with real timestamp?

or generally is there any other sensible method around this problem?

解决方案

You're probably solving the X/Y problem here, and you might be better off explaining your use case and getting a better overall solution; fetching a server timestamp to use on the client seems extremely likely to be a conceptual problem.

For example, if my goal is just to display the time in messages, I can simple call set, monitor the path, and display what it returns:

var ref = new Firebase(URL);
ref.on('child_added', function(snap) {
   // client just listens on path, records have serve timestamp when they arrive
   console.log('the last event was added at', snap.val().time);
});
ref.push({ name: 'Kato', time: Firebase.ServerValue.TIMESTAMP });

If you are queuing future events, just store them in a different path and move them to the "present" path when ready. Rather than trying to fetch "now" and insert it later.

If you are set on fetching the timestamp, the simplest way would be to set up a dummy path and set the value against it:

new Firebase(URL).transaction(function(currValue) {
   return Firebase.ServerValue.TIMESTAMP;
}, function(err, success, snap) {
   console.log('the current server timestamp', snap.val());
});

这篇关于未来的时间戳作为火力点的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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