0

I am experiencing unexplained behaviour with the Android ChildEventListener.

I have a ChildEventListener setup on the "game" node with the onChildAdded, onChildChanged and onChildRemoved callbacks setup:

aQuery = mDatabase.child(Constants.FIREBASE_DEV_DATA_CHILD).child("aNode").child("game");

However, when I write to the "game" node with the following...

mDatabase.child(Constants.FIREBASE_DEV_DATA_CHILD).child("aNode").child("game").push().setValue(createGame, new DatabaseReference.CompletionListener()

... it is firing both the onChildAdded and onChildChanged callbacks. The dataSnapshot is the same for both. I don't understand why that is happening? Shouldn't just the onChildAdded fire when I write?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
brndn
  • 521
  • 1
  • 4
  • 18
  • What's `createGame`? In general: try to reproduce the problem with a single snippet of code with only hard-coded values. That makes it the easiest for us to see what's going on, or try and reproduce the problem. – Frank van Puffelen Sep 06 '17 at 02:23
  • createGame is a custom class with an assortment of Strings, Doubles and LatLng's. I'll try writing a simple string and report back. – brndn Sep 06 '17 at 05:47
  • Okay after some testing, it appears to be when I use createGame.created_timestamp = ServerValue.TIMESTAMP. If I don't use ServerValue.TIMESTAMP the callback behaves as expected. – brndn Sep 06 '17 at 06:11
  • Ah yes, a timestamp would indeed explain (which is why I asked what `createGame` is). See my answer here: https://stackoverflow.com/questions/37864974/how-to-use-the-firebase-server-timestamp-to-generate-date-created/37868163#37868163 – Frank van Puffelen Sep 06 '17 at 14:24
  • Thanks @FrankvanPuffelen, your break down of execution steps in your linked thread makes a lot of sense. I don't quite follow the link you provided as a method of dealing with it though: https://stackoverflow.com/questions/33096128/when-making-a-pojo-in-firebase-can-you-use-servervalue-timestamp/33111791#33111791 - This won't stop the onChildChanged being fired for new writes, will it? – brndn Sep 12 '17 at 03:35

0 Answers0