Firebase ServerValue.TIMESTAMP in Java data models objects

前端 未结 6 1322
[愿得一人]
[愿得一人] 2020-11-30 10:42

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:

相关标签:
6条回答
  • 2020-11-30 11:01

    Kotlin provides an easy way to achieve this by 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 by

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

    This will get default values from data class.

    Or even you can initiate your own values by

    val firebaseModel = FirebaseRequestModel(ServerValue.TIMESTAMP, 2134, 0)
    firebaseRef.setValue(firebaseModel)
    
    0 讨论(0)
  • 2020-11-30 11:03

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

    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;
    }
    

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

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

    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");
    }
    
    0 讨论(0)
  • 2020-11-30 11:03

    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;
        }
    }
    

    And so:

    public static void start(Context context) {
        MyTimeStamp timeStamp = new MyTimeStamp();
        timeStamp.setTimestamp(ServerValue.TIMESTAMP);
    
        Log.d(TAG, "start: ", timeStamp.getTimestamp().toString());
    }
    
    0 讨论(0)
  • 2020-11-30 11:05

    Here's how I do it

    //member variable
    Object createdTimestamp;
    
    public YourConstructor(){
        createdTimestamp = ServerValue.TIMESTAMP
    }
    
    @Exclude
    public long getCreatedTimestampLong(){
        return (long)createdTimestamp;
    }
    
    0 讨论(0)
  • 2020-11-30 11:15

    Similar to Urgurcan's answer, but a bit cleaner so the caller doesn't have trouble guessing between getTimestamp vs timestamp.

    public class FirebaseDbObject {
    
        private Object timestamp = ServerValue.TIMESTAMP;
    
        //........
        //........
        @PropertyName("timestamp")
        Object getRawTimestamp() {
            return timestamp;
        }
    
        @Exclude
        public long getTimestamp() {
            return (long) timestamp;
        }
    
    }
    
    0 讨论(0)
  • Your db object should include these:

    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 "timestamp" to your object.

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

    0 讨论(0)
提交回复
热议问题