I\'ve looked at a few other answers for similar problems but don\'t understand why mine isn\'t working.
I\'m trying to get my app to read commands from Firebase and
The solution provided by Alex worked but didn't quite answer why I couldn't get store my data in an object, and for my app while the child added listener, the app is able to read data that was already on Firebase but when new child is added, the value is empty.
onChildAdded:DataSnapshot { key = -L0JjGo-3QMYDsuTMQcN, value = }
I've done a little more digging to find out what might be causing this and found that it is probably caused because the values were not all written at the same time. And looking at firebase it seems that the key is added first, then the values are added. This is why the error says can't convert, it is because it doesn't exist. I'm using the Python Firebase Admin SDK to add the data to Firebase, so I'm not sure if this is the reason for that.
So to fix my problem I moved my code to the onChildChanged function and added a check so that the code only runs when all of the data I need exists. This way I can get the values stored in an object straight away.
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName){
if (dataSnapshot.child("text").exists() &&
dataSnapshot.child("executed").exists() &&
dataSnapshot.child("timestamp").exists()){
Log.d(TAG, "onChildChanged:" + dataSnapshot.toString());
CommandObject command = dataSnapshot.getValue(CommandObject.class);
if ("TAKE_OFF".equals(command.text)) {
mMiniDrone.takeOff();
} else if ("LAND".equals(command.text)) {
mMiniDrone.land();
}
}
}