Reputation: 5119
I read in this answer about ServerValue.TIMESTAMP
that the ValueEventListener
will fire twice with first the local time and then with server time.. My question is will that only happen if the code look like this using setValue
:
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot.getValue());
}
public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);
Or will it also happen if the call is a updateChildren
like this, notice the ServerValue.TIMESTAMP
Map<String, Object> someMap = new HashMap<>();
someMap.put("id", 239231);
someMap.put("time", ServerValue.TIMESTAMP);
someMap.put("name", "some name");
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("someKey", someMap);
mFirebase.updateChildren(childUpdates, new CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
}
});
UPDATE
Here´s what I try to do.
In the Firebase code lab for Friendly Chat app(friendlychat). I replace the send chat message code that look like this:
FriendlyMessage friendlyMessage = new
FriendlyMessage(mMessageEditText.getText().toString(),
mUsername,
mPhotoUrl,
null /* no image */);
mFirebaseDatabaseReference.child(MESSAGES_CHILD)
.push().setValue(friendlyMessage);
with an childUpdates
instead like this:
final String pushKeyAddress = mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().getKey();
Map<String, Object> someMap = new HashMap<>();
someMap.put("text", mMessageEditText.getText().toString());
someMap.put("name", mUsername);
someMap.put("photoUrl", mPhotoUrl);
someMap.put("time", ServerValue.TIMESTAMP);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(MESSAGES_CHILD.concat("/").concat(pushKeyAddress), someMap);
mFirebaseDatabaseReference.updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
System.out.println(databaseError);
}
});
And I see the CompletionListener
onComplete
only fires once and not twice as I would expect since I use the ServerValue.TIMESTAMP
Upvotes: 0
Views: 531
Reputation: 598797
My answer (which you link in the question) explains what happens in "in a write operation". The same thing will happen for either a setValue()
and an updateChildren()
.
Upvotes: 1