Firebase ServerValue.TIMESTAMP in Java Data Model Objects

I'm new to Firebase, and so far I really enjoyed it. I have a problem's; I am using FirebaseListAdapter, similar to the tutorial diagram here: https://github.com/firebase/AndroidChat

To use the FirebaseListAdapter, I need to use data model objects (for auto-binding to work well). The problem is that I also want to save the timestamp value with this model object, and I want to get the timestamp from the Firebase server.

I am not currently working, this is a DataModelObject class (similar to com.firebase.androidchat.Chat in a demo) with a constructor like:

DataModelObject(String data1, String data2, Map enQTimeStamp) 

which I then try to use as follows:

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

This throws a JsonMappingException when I try to run this code. I found the code snippet here:

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

But it’s worth noting that in line 4 of the Android code example, this will cause a compile-time error (since it tries to put ServerValue.TIMESTAMP on the card, and TIMESTAMP is the card itself)

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

+8
source share
7 answers

This is similar to this question: When creating POJOs in Firebase, can you use ServerValue.TIMESTAMP?

When creating POJOs used to store / retrieve data other than an empty default constructor, I usually use a constructor like 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; } 

Be sure to enable the receiver for the HashMap <> used to store the timestamp:

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

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

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

This is how i do it

 //member variable Object createdTimestamp; public YourConstructor(){ createdTimestamp = ServerValue.TIMESTAMP } @Exclude public long getCreatedTimestampLong(){ return (long)createdTimestamp; } 
+11
source

Your db object should include the following:

 public class FirebaseDbObject { private final Object timestamp = ServerValue.TIMESTAMP; //........ //........ Object getTimestamp() { return timestamp; } @Exclude public long timestamp() { return (long) timestamp; } } 

This will add an extra field called a timestamp to your object.

Edit: The answer posted by MobileMon is not entirely correct, as it does not have a getter method. This is a complete and correct answer.

+3
source

Kotlin provides an easy way to achieve this using data classes. You can create it like

 data class FirebaseRequestModel( var start_time: Any = ServerValue.TIMESTAMP, var stop_time: Long = 0, var total_time: Long = 0, ) 

and use it directly

 val firebaseModel = FirebaseRequestModel() firebaseRef.setValue(firebaseModel) 

This will get the default values ​​from the data class.

Or even you can initiate your own values ​​by

 val firebaseModel = FirebaseRequestModel(ServerValue.TIMESTAMP, 2134, 0) firebaseRef.setValue(firebaseModel) 
0
source

Similar to Urgurkan’s answer, but a little cleaner, so it’s easy for the caller to guess between getTimestamp and timestamp.

 public class FirebaseDbObject { private Object timestamp = ServerValue.TIMESTAMP; //........ //........ @PropertyName("timestamp") Object getRawTimestamp() { return timestamp; } @Exclude public long getTimestamp() { return (long) timestamp; } } 
0
source

You can do it:

 public class MyTimeStamp { private Object timestamp; public MyTimeStamp() { } public Object getTimestamp() { return timestamp; } public void setTimestamp(Object timestamp) { this.timestamp = timestamp; } } 

So:

 public static void start(Context context) { MyTimeStamp timeStamp = new MyTimeStamp(); timeStamp.setTimestamp(ServerValue.TIMESTAMP); Log.d(TAG, "start: ", timeStamp.getTimestamp().toString()); } 
-one
source

hhhhhhhhhhhhh

ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp

-4
source

All Articles