目前,Google的ServerValue.TIMESTAMP退货版本将{".sv":"timestamp"}用作Firebase的指令,以便在将数据保存到Firebase服务器后使用服务器时间戳填充该字段.
但是,当您在客户端创建数据时,您还没有实际的时间戳(即用作创建日期).在初始保存和随后的检索之后,您只能访问时间戳,我想 - 有时候太晚了,不太优雅.
在Google之前:
更新:忽略此部分,因为它不正确 - 我误解了这些例子.ServerValue.TIMESTAMP总是回来的{".sv":"timestamp"}.
据我所知,在google之前的Firebase中,似乎有一个服务器生成的时间戳可用于获取实际时间戳:
import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP // eg. 1466094046
Run Code Online (Sandbox Code Playgroud)
问题:
注意:
我不打算new Date()在客户端使用,因为我一直在阅读它并不安全,但如果你认为不同,请分享你的想法.
我是Firebase的新手,到目前为止我一直非常喜欢它.我遇到了问题; 我正在使用类似于教程大纲的FirebaseListAdapter:https://github.com/firebase/AndroidChat
要使用FirebaseListAdapter,我需要使用数据模型对象(以使自动绑定工作得很好).问题是我还想保留该模型对象的时间戳值,并且我想从Firebase服务器获取时间戳.
我目前没有工作的是一个类DataModelObject(类似于演示示例中的com.firebase.androidchat.Chat),其构造函数如下:
DataModelObject(String data1, String data2, Map enQTimeStamp)
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用这样:
DataModelObject dmo = new DataModelObject ("foo", "bar", ServerValue.TIMESTAMP);
myFirebaseRef.push().setValue(dmo);
Run Code Online (Sandbox Code Playgroud)
当我尝试运行该代码时,这会导致JsonMappingException.我在这里找到了一个代码片段:
https://www.firebase.com/blog/2015-02-11-firebase-unique-identifiers.html
但是值得注意的是,在Android代码示例的第4行,这将导致编译时错误(因为他试图将ServerValue.TIMESTAMP放入Map中,而TIMESTAMP是Map本身)
正确的方法是什么,并保持与FirebaseListAdapter的兼容性?
我正在尝试修改Firebase的Android聊天示例以包含Firebase时间戳值.我可以使用时间戳发送,ServerValue.TIMESTAMP;但是当Firebase尝试显示消息时,应用程序崩溃了.编辑:下面的完整错误输出
发送我使用的消息
private void sendMessage() {
EditText inputText = (EditText)findViewById(R.id.messageInput);
String input = inputText.getText().toString();
Map timestamp = ServerValue.TIMESTAMP;
if (!input.equals("")) {
// Create our 'model', a Chat object
Chat chat = new Chat(name, input, timestamp, userID);
// Create a new, auto-generated child of that chat location, and save our chat data there
chatRef.push().setValue(chat);
inputText.setText("");
}
}
Run Code Online (Sandbox Code Playgroud)
伪造的结构是这样的:
->Messenger
|--> room
|--> messages
|--> messageID
|--> from: "Name"
|--> text: "Message"
|--> timestamp: xxxxxxxxxxxxx
|--> userID: "id"
Run Code Online (Sandbox Code Playgroud)
和Chat.java
public …Run Code Online (Sandbox Code Playgroud)