firebase - Using ServerValue.TIMESTAMP in my Android App -


i have read lots of stackoverflow questions related to

servervalue.timestamp 

but can't figure out how use in app.

i need timestamp of creation of post. timestamp should added same place uid , author etc. of post.

code snippet writes post firebase database:

 private void writenewpost(string userid, string username, string title, string body) {     // create new post @ /user-posts/$userid/$postid , @     // /posts/$postid simultaneously     string key = mdatabase.child("posts").push().getkey();     post post = new post(userid, username, title, body);     map<string, object> postvalues = post.tomap();      map<string, object> childupdates = new hashmap<>();     childupdates.put("/posts/" + key, postvalues);     childupdates.put("/user-posts/" + userid + "/" + key, postvalues);      mdatabase.updatechildren(childupdates); } 

my post.java file

@ignoreextraproperties public class post {  public string uid; public string author; public string title; public string body; public int starcount = 0; public map<string, boolean> stars = new hashmap<>(); public long timestamp;  public post() {     // default constructor required calls datasnapshot.getvalue(post.class) }  public post(string uid, string author, string title, string body) {     this.uid = uid;     this.author = author;     this.title = title;     this.body = body; }  // [start post_to_map] @exclude public map<string, object> tomap() {     hashmap<string, object> result = new hashmap<>();     result.put("uid", uid);     result.put("author", author);     result.put("title", title);     result.put("body", body);     result.put("starcount", starcount);     result.put("stars", stars);      return result; } // [end post_to_map] 

}

how should use

servervalue.timestamp  

in code time of creation of post.

servervalue.timestamp placeholder value. when firebase server processes update request , finds servervalue.timestamp value, replaces current server clock.

in writenewpost() method add line set creation time:

map<string, object> postvalues = post.tomap(); postvalues.put("timestamp", servervalue.timestamp); 

if use post.tomap() create posts, , not updates, put similar statement there instead of in writenewpost().


Comments