Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new child in Firebase database Android

I'm trying to add a new child using the DatabaseReference in my Firebase Android app. I'm doing:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() 
mDatabase.child("childToAdd").setValue(1);

I can do this with a regular Firebase Reference as it would add the child to the database if it isn't there.
How could I go about doing this with DatabaseReference?

Edit: Thanks for all the suggestions but I'm having issues with the following code. When it enters the if block it does not push the data onto the database.

https://gist.github.com/rounaksalim95/a5cba332400c6caf8320f15b0cbf06e8

When I try this with the old Firebase reference I get error code 11 (User code called from firebase runloop) and with the new database reference I get error code -3 (PermissionDenied even though I have no security rules).

Update:

I got it to do what I wanted to using a single value event listener:

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 database reference doesn't let me add values like this.

like image 855
Ron7 Avatar asked Oct 13 '16 05:10

Ron7


People also ask

How do I add a child to Firebase?

Add children$newKey = $firebase->push([ 'child_key_1' => 'value_1', 'child_key_2' => 'value_2', ], 'my/data'); $newKey will be populated with the name of the new child, which is generated by the Firebase database, e.g. -KMkDDGJQ9vuooQapvdv .

How do I manually add data to Firebase?

If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign.

How do I move Firebase child from one node to another in Android?

If you have the ID of your child, then you can move from one ID directly to the other one. It would like : myfirebase.firebase.com/initialPlace/ID to myfirebase.firebase.com/finalPlace/ID . You can pass any string as a variable to the firebase constructor.


2 Answers

You'll need to call push() to generate a unique key for the new data:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() 
mDatabase.push().setValue(1);

This will append the new value at the end of the list.

By combining the push() and child() methods, you can create multiple lists of children in the database:

mDatabase.child("numbers").push().setValue(1);
mDatabase.child("numbers").push().setValue(53);
mDatabase.child("numbers").push().setValue(42);

mDatabase.child("letters").push().setValue("a");
mDatabase.child("letters").push().setValue("z");
mDatabase.child("letters").push().setValue("c");

See the section append to a list of data in the Firebase documentation.

like image 81
Frank van Puffelen Avatar answered Sep 22 '22 05:09

Frank van Puffelen


Another post mentioned to check your gradle app file to ensure you have the latest version of the Firebase as follows:

compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'

I think this should fix many issues that could arise from using older versions.

I hope this helps.

like image 20
seansanders Avatar answered Sep 22 '22 05:09

seansanders