0

I use the following code to add a timestamp to a message:

Map<String, Object> postValues = post.toMap();
postValues.put("timeStamp", ServerValue.TIMESTAMP);

On the client side two timestamp values are printed out (one for the local creation date and one for the value from the server I think). How do I only print out the value which came from the server?

Side-note for Frank: The code below is called when the user presses the 'send' button. Thus, the messageViewHolder gets updated twice in my app (see related question). The viewHolder is updated once with the local time estimate and once with the server time. I want it to update the holder only with the server time (once) for practical purposes.

FriendlyMessage friendlyMessage = new FriendlyMessage("bla","bla","bla");

 String key = mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().getKey();
 Map<String, Object> postValues =  friendlyMessage.toMap();
 postValues.put("creationDate", ServerValue.TIMESTAMP);
 childUpdates.put("/"+MESSAGES_CHILD+"/" + key, postValues);
 Map<String, Object> childUpdates = new HashMap<>();

 mFirebaseDatabaseReference.updateChildren(childUpdates);
  • Your question probably needs more elaboration. What do you mean by 2 timestamps ? what are you trying to do ? are you sending this message to server and getting a data back ? – zeekhuge Jul 01 '17 at 10:57
  • @ZeekHuge Yes, I'm sending the message and getting the data back –  Jul 01 '17 at 10:58
  • @ZeekHuge But I receive two timestamps approx. 300 ms apart. Please reply. –  Jul 01 '17 at 10:59
  • That really depends on the server APIs you are using. If that APIs work is to simply append another parameter to the given data. It will do just that. – zeekhuge Jul 01 '17 at 11:00
  • but, maybe you could just check which timestamp is the later one, and use it as the one from the server. If it has resolution of upto ms, then the one from server will definitely be the later one. – zeekhuge Jul 01 '17 at 11:02
  • @ZeekHuge That's exactly what I want to do programmatically. Can you please write an answer detailing how to check which timestamp is the latter one? –  Jul 01 '17 at 11:04
  • whats the format of the output ? maybe you should add that to your question. – zeekhuge Jul 01 '17 at 11:08
  • @ZeekHuge it's a long –  Jul 01 '17 at 11:14
  • wont be able to help then. maybe you could just post the relevant part of it. – zeekhuge Jul 01 '17 at 11:16
  • This is the [expected behavior](https://stackoverflow.com/a/37868163/209103): the first value is the local estimate, the second it the authoritative server-side response. There is no way to only get the second value, although you can certainly write code to ignore the first value. But this sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): what are you trying to accomplish that requires you to ignore the local estimate? – Frank van Puffelen Jul 01 '17 at 15:31
  • @FrankvanPuffelen Hey Frank, thanks for helping. I edited the question for you, please take a look :) It's pretty complicated and unique! –  Jul 01 '17 at 15:42
  • @FrankvanPuffelen I did *not* find a solution to my problem online. I tried asking for support from the Firebase team and they told to ask my question on SO. –  Jul 01 '17 at 15:45
  • Your latest code snippet will still get two values: the local estimate, and the server-side authoritative one. So it will update the view twice. You could ignore the first value, but it'll be less-than-pretty indeed - since you're essentially trying to go against the flow of the API. What's the problem about updating the view twice? – Frank van Puffelen Jul 01 '17 at 16:02
  • @FrankvanPuffelen When a local timestamp is created, the view updates and a value is sent to the server **too early** which creates problems in my own app's logic. –  Jul 01 '17 at 16:11
  • @FrankvanPuffelen I just want to update the holder once - with the server timestamp. –  Jul 01 '17 at 16:12
  • Did you put a simple `if` in the value listener to ignore the initial value? – Frank van Puffelen Jul 01 '17 at 16:24
  • @FrankvanPuffelen I found another way to deal with my issue. Here's the related question: https://stackoverflow.com/questions/44863779/add-child-using-cloud-functions –  Jul 01 '17 at 17:59

0 Answers0