I am trying to add a new child using DatabaseReferencein my Firebaseandroid app. I do:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference()
mDatabase.child("childToAdd").setValue(1);
I can do this with a regular directory Firebase, as it will add a child to the database if it is not there.
How could I do this using DatabaseReference?
Edit: Thanks for all the suggestions, but I am having problems with the following code. When it enters an if block, it does not output data to the database.
https://gist.github.com/rounaksalim95/a5cba332400c6caf8320f15b0cbf06e8
When I try to do this with the old Firebase link, I get an error code 11 (user code called from runbop firebase), and with a new database link I get an error code -3 (PermissionDenied, although I don't have security rules).
Update:
I got it to do what I wanted to use to listen to one event:
Firebase userRef = new Firebase(BASEURL + "userData");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(user.getUid()).getValue() == null) {
userRef.child(user.getUid()).setValue(1);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
However, the database link does not allow me to add such values.
source
share