0

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

Erik
  • 5,039
  • 10
  • 63
  • 119

1 Answers1

1

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().

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • in my `updateChildren` example code above I have one HashMap `someMap`. What if there is 2 like `someMap1` and `someMap2` targeting dif locations in Firebase-databas, and they both have a `("time", ServerValue.TIMESTAMP);`, will that make the onComplete() trigger four times?? – Erik Aug 10 '17 at 22:13
  • Each write of `ServerValue.TIMESTAMP` will follow the same flow as in my linked answer. Is that not the behavior you see when you run the code? – Frank van Puffelen Aug 11 '17 at 01:01
  • I must do something wrong the `updateChildren` `onComplete(..)` only fires once but the `("time", ServerValue.TIMESTAMP)` key in Firebase is set ok. This is not a problem I just wanted to understand this. I set a breakpoints and even do `System.out.println(dataSnapshot.getValue()); ` Maybe it´s because ` setPersistenceEnabled(true) ` ..mmm – Erik Aug 11 '17 at 08:00
  • I update my Q with specifics please have a look and say what you think – Erik Aug 11 '17 at 10:35