Java数据模型对象中的Firebase ServerValue.TIMESTAMP [英] Firebase ServerValue.TIMESTAMP in Java data models objects

查看:90
本文介绍了Java数据模型对象中的Firebase ServerValue.TIMESTAMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的新手,到目前为止我真的很喜欢它.我遇到了问题;我正在使用类似于此处的教程概述的FirebaseListAdapter: https://github.com/firebase/AndroidChat

I'm new to Firebase, and I've been really enjoying it so far. I'm running into a problem; I'm using the FirebaseListAdapter similar to the tutorial outline here: https://github.com/firebase/AndroidChat

要使用FirebaseListAdapter,我需要使用数据模型对象(以使自动绑定正常工作).问题是我还希望与该模型对象一起保留时间戳记值,并且我想从Firebase服务器获取时间戳记.

To use the FirebaseListAdapter, I need to use data model objects (to get the automatic binding to work nicely). The problem is I also want to keep a timestamp value with that model object, and I want to get the timestamp from the Firebase server.

我目前无法使用的是类DataModelObject(类似于演示示例中的com.firebase.androidchat.Chat),其构造函数如下:

What I have currently that is NOT working is a class DataModelObject (similar to com.firebase.androidchat.Chat in the demo example) with a constructor like :

DataModelObject(String data1, String data2, Map enQTimeStamp)

然后我尝试像这样使用:

which I then try to use like this:

DataModelObject dmo = new DataModelObject ("foo", "bar", ServerValue.TIMESTAMP);
myFirebaseRef.push().setValue(dmo);

当我尝试运行该代码时,这将导致JsonMappingException.我在这里找到了一个代码段:

This causes a JsonMappingException when I try to run that code. I found a code snippet here :

https://www.firebase.com/blog/2015-02-11-firebase-unique-identifiers.html

但是值得注意的是,在Android代码示例的第4行上,这将导致编译时错误(因为他正尝试将ServerValue.TIMESTAMP放入Map中,而TIMESTAMP本身就是Map)

But it's worthwhile to note that on line 4 of the Android code example, that will cause a compile time error (as he is trying to put ServerValue.TIMESTAMP into a Map, and TIMESTAMP is a Map itself)

执行此操作并保持与FirebaseListAdapter兼容性的正确方法是什么?

What is the right way to do this and maintain compatibility with FirebaseListAdapter?

推荐答案

这听起来类似于以下问题:

This sounds similar to this question: When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

在创建用于存储/检索数据的POJO时,除了默认的空构造函数外,我通常使用类似于以下的构造函数:

When creating POJOs used to store/retrieve data apart from the default empty constructor I usually use a constructor similar to this:

Param param1;
Param param2;
HashMap<String, Object> timestampCreated;

//required empty constructor
public DataObject(){}

public DataObject(Param param1, Param param2) {
       this.param1 = param1;
       this.param2 = param2;
       HashMap<String, Object> timestampNow = new HashMap<>();
       timestampNow.put("timestamp", ServerValue.TIMESTAMP);
       this.timestampCreated = timestampNow;
}

确保为用于存储时间戳的HashMap包含一个吸气剂:

Be sure to include a getter for the HashMap<> used to store the Timestamp:

public HashMap<String, Object> getTimestampCreated(){
    return timestampCreated;
}

然后使用@Exclude批注创建一个getter,您可以在代码中使用该getter以获取时间戳的值(如果需要). @Exclude注释将使Firebase忽略此吸气剂,而不会寻找相应的属性

Then use the @Exclude annotation to create a getter that you can use in your code to get the value of the timestamp if you need it. The @Exclude annotation will cause Firebase to ignore this getter and not look for a corresponding property

@Exclude
public long getTimestampCreatedLong(){
    return (long)timestampCreated.get("timestamp");
}

这篇关于Java数据模型对象中的Firebase ServerValue.TIMESTAMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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