When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

前端 未结 6 1103
挽巷
挽巷 2020-11-30 02:51

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 va

6条回答
  •  失恋的感觉
    2020-11-30 03:05

    The most common use for Server.TIMESTAMP is:

    1. set the server value when the data is SENT into the server
    2. fetch this model from firebase,and cast it easily to Long

    This trick saved me the hard work of handle 2 different fields for only 1 value

        public class HandleTimestampFirebaseObject {
    
        Object timestamp;
    
        public HandleTimestampFirebaseObject() {
    
        }
    
        public Object getTimestamp(){
            if(timestamp instanceof Double){
    
          /*    this will handle the case of AFTER fetch from backend,and  
                serialize to object into SharedPreferences for example ,when the 
                Object casting automatically into Double.
                (Anyway,call (long)getTimestamp from your code who use this model*/
                return ((Double) timestamp).longValue();
            }
            else{
                return timestamp;  //this handle to firebase requirement in the server side(Object needed)
            }
        }
    

提交回复
热议问题