在Firebase中制作POJO时,您可以使用ServerValue.TIMESTAMP吗? [英] When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

查看:106
本文介绍了在Firebase中制作POJO时,您可以使用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



我找不到一种方法来处理 long HashMap< String,String> ,但是如果你将这个属性嵌套到更通用的 HashMap< String,Object> (date,1443765561874)或者作为 ServerValue.TIMESTAMP 哈希映射(date,{.sv,servertime} )。然后当你把它拉出来时,它总是一个带有(date,some long number)的HashMap。然后,您可以使用 @JsonIgnore @Exclude注释(这意味着Firebase将忽略它,不要将其视为序列化数据库的方法)在POJO类中创建一个帮助器方法,以轻松获取

POJO类的完整示例如下:

<$>
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-c​​rashes-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);
}

}


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?

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.

In the POJO class, you might have:

private String timeLastChanged;

or

private Map<String, String> timeLastChanged;

In the first example with the String, I run into the issue of setting timeLastChange = ServerValue.TIMESTAMP;, because ServerValue.TIMESTAMP is a 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?

解决方案

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 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.

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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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