问题描述
当您制作要从 Firebase 序列化和反序列化到 Firebase 的普通旧 Java 对象时,有没有办法使用 ServerValue.TIMESTAMP
值?
When you're making a Plain Old Java Object that's meant to be serialized from and deserialized to Firebase, is there a way to use the ServerValue.TIMESTAMP
value?
例如,假设我想要一个对象,其中一个属性是上次编辑时的属性,并且您想使用 ServerValue.TIMESTAMP
值.
For example, let's say I want to have an object where one of the properties is the last time it was edited and you'd like to use the ServerValue.TIMESTAMP
value.
在 POJO 类中,您可能有:
In the POJO class, you might have:
private String timeLastChanged;
或
private Map<String, String> timeLastChanged;
在带有 String
的第一个示例中,我遇到了设置 timeLastChange = ServerValue.TIMESTAMP;
的问题,因为 ServerValue.TIMESTAMP
是一张地图.
In the first example with the String
, I run into the issue of setting timeLastChange = ServerValue.TIMESTAMP;
, because ServerValue.TIMESTAMP
is a Map.
在带有 Map<String, String>
的第二个示例中,我收到无法去抖动"错误,因为它无法将存储在数据库中的 long 正确反序列化为 Map<字符串,字符串>
.有没有办法解决这个问题?
In the second example with the Map<String, String>
I get a "failed to debounce" error because it can't properly deserialize the long stored in the database into a Map<String, String>
. Is there any work around for this?
推荐答案
@Exclude 注释在您的 POJO 类中创建一个辅助方法(这意味着 Firebase 将忽略它,而不是将其视为与数据库进行序列化的方法)以轻松获取返回的 HashMap 中要在您的应用中使用的 long 值.
I couldn't figure out a way to deal with the conversion between long
and HashMap<String, String>
, but if you nest the property in a more generic HashMap<String, Object>
it can go into the database as either a single long value ("date", "1443765561874") or as the ServerValue.TIMESTAMP
hash map ("date", {".sv", "servertime"}). Then when you pull it out, it will always be a HashMap with ("date", "some long number"). You can then create a helper method in your POJO class using the @Exclude annotation (meaning Firebase will ignore it and not treat it as a method for serializing to/from the database) to easily get the long value from the returned HashMap to use in your app.
POJO 类的完整示例如下:
Full example of a POJO class is below:
import com.google.firebase.database.Exclude;
import com.firebase.client.ServerValue;
import java.util.HashMap;
import java.util.Map;
public class ExampleObject {
private String name;
private String owner;
private HashMap<String, Object> dateCreated;
private HashMap<String, Object> dateLastChanged;
/**
* Required public constructor
*/
public ExampleObject() {
}
public ExampleObject(String name, String owner, HashMap<String,Object> dateCreated) {
this.name = name;
this.owner = owner;
this.dateCreated = dateCreated;
//Date last changed will always be set to ServerValue.TIMESTAMP
HashMap<String, Object> dateLastChangedObj = new HashMap<String, Object>();
dateLastChangedObj.put("date", ServerValue.TIMESTAMP);
this.dateLastChanged = dateLastChangedObj;
}
public String getName() {
return name;
}
public String getOwner() {
return owner;
}
public HashMap<String, Object> getDateLastChanged() {
return dateLastChanged;
}
public HashMap<String, Object> getDateCreated() {
//If there is a dateCreated object already, then return that
if (dateCreated != null) {
return dateCreated;
}
//Otherwise make a new object set to ServerValue.TIMESTAMP
HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
dateCreatedObj.put("date", ServerValue.TIMESTAMP);
return dateCreatedObj;
}
// Use the method described in https://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#25512747
// to get the long values from the date object.
@Exclude
public long getDateLastChangedLong() {
return (long)dateLastChanged.get("date");
}
@Exclude
public long getDateCreatedLong() {
return (long)dateCreated.get("date");
}
}
这篇关于在 Firebase 中制作 POJO 时,您可以使用 ServerValue.TIMESTAMP 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!