在Firebase中制作POJO时,您可以使用ServerValue.TIMESTAMP吗? [英] When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?
问题描述
在制作一个普通的旧Java对象时,这个对象应该被序列化并反序列化为Firebase,有没有办法使用 ServerValue.TIMESTAMP
值? / p>
例如,假设我想拥有一个对象,其中一个属性是最后一次编辑的对象,并且您希望使用 ServerValue.TIMESTAMP
value。
在POJO类中,可能有:
private String timeLastChanged;
或
私人地图< String,String> timeLastChanged;
在第一个使用 String
的例子中,我遇到了设置 timeLastChange = ServerValue.TIMESTAMP;
的问题,因为 ServerValue.TIMESTAMP
是一个Map。在$ Map< String,String>
的第二个例子中,我得到了一个未能消除抖动的错误,因为它可以将存储在数据库中的long正确反序列化为 Map< String,String>
。是否有任何解决这个问题的方法?
将@JsonIgnore转换为 @排除,正如很多人所说的。
我终于想出了一个灵活的解决方案使用日期和ServerValue.TIMESTAMP。这是来自 Ivan V 的示例, Ossama 和 PUF 。
我找不到一种方法来处理 POJO类的完整示例如下: 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 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 In the POJO class, you might have: or In the first example with the In the second example with the Update 12/27/2016 Switched out @JsonIgnore for @Exclude as many have mentioned. I finally came up with a flexible solution for working with Dates and ServerValue.TIMESTAMP. This is working off of examples from Ivan V, Ossama, and puf. I couldn't figure out a way to deal with the conversion between Full example of a POJO class is below:
这篇关于在Firebase中制作POJO时,您可以使用ServerValue.TIMESTAMP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! long
和 HashMap< String,String>
,但是如果你将这个属性嵌套到更通用的 HashMap< String,Object> (date,1443765561874)或者作为
ServerValue.TIMESTAMP
哈希映射(date,{.sv,servertime} )。然后当你把它拉出来时,它总是一个带有(date,some long number)的HashMap。然后,您可以使用
<$> p $ p> 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;
私人字符串拥有者;
私有HashMap< String,Object>创建日期;
私有HashMap< String,Object> dateLastChanged;
$ b / **
*必需的公共构造函数
* /
public ExampleObject(){
}
public ExampleObject String name,String owner,HashMap< String,Object> dateCreated){
this.name = name;
this.owner = owner;
this.dateCreated = dateCreated;
//上次更改的日期将始终设置为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(){
//如果已经有一个dateCreated对象,那么返回
if(dateCreated!= null){
return dateCreated;
}
//否则将一个新的对象设置为ServerValue.TIMESTAMP
HashMap< String,Object> dateCreatedObj = new HashMap< String,Object>();
dateCreatedObj.put(date,ServerValue.TIMESTAMP);
return dateCreatedObj;
}
//使用https://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#中描述的方法25512747
//从日期对象中获取长整型值。
@Exclude
public long getDateLastChangedLong(){
return(long)dateLastChanged.get(date);
}
@Exclude
public long getDateCreatedLong(){
return(long)dateCreated.get(date);
}
}
ServerValue.TIMESTAMP
value?ServerValue.TIMESTAMP
value.private String timeLastChanged;
private Map<String, String> timeLastChanged;
String
, I run into the issue of setting timeLastChange = ServerValue.TIMESTAMP;
, because ServerValue.TIMESTAMP
is a Map.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?
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 @JsonIgnore @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.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");
}
}