Another event triggering problem from Firebase.
I have the class Post which contain a properties that store Timestamp like below:
public class Post {
public int likeCount;
private Long createDate;
public String createBy;
public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}
@Exclude
public Long getCreateDateLong() {
return createDate;
}
public Post() {
}
}
When i add a new post to firebase-database like below:
Post newPost = new Post();
newPost.createBy = firebaseAuth.getCurrentUser().getUid();
databaseReference.child("posts").push().setValue(newPost);
The onDataChanged for ValueEventListener() will trigger twice, and the onChildAdded and onChildChanged for ChildEventListener() will be triggered one after another.
I suspect this is because the ServerValue.TIMESTAMP actually caused the createDate to update after the creation of the child.
This is a very annoying and unexpected behaviour to deal with. It should have trigger just once.
I'm getting sick of these problems. Is it a right decision to go with Firebase Database in the first place? sigh...
[Update]
The behavior is clearly explained by Frank here.
However, is there common pattern to handle this behavior? In the situation that I have the listener setup and do something to the UI when data change. Then I insert a new record with a timestamp in it. What happen next is that my UI actually have to do something twice for one single insert event.
I understand that there is a addListenerForSingleValueEvent, but the reason i use don't it is because i also handle update of data from anywhere similarly. If I use addListenerForSingleValueEvent i will have to remove the ValueEventListener before i insert, then add it back after insert done. This doesn't seems right to me.
Is there anything in the event callback that i can use to determine weather or not it is a change that i care and want to do something about? Did i missed something?