How to add a new child to the Firebase Android database

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.

+4
source share
7 answers

You need to call push()to create a unique key for the new data:

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

This will add a new value at the end of the list.

By combining the push()and methods child(), you can create several lists of child elements 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");

. Firebase.

+11

, gradle, , ​​ Firebase :

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

, , .

, .

+2

push() firebase. , .

+1

deepak , , . , , , , .

private DatabaseReference childRef;
private DatabaseReference listRef;

databaseReference = FirebaseDatabase.getInstance().getReference();
childRef = databaseReference.child("deepak");
listRef = childRef.child("list");
listRef.setValue(callList);
0

, .

     public class FirebaseUserDetails
    {
    private String mDisplayName;
    public String getmDisplayName() {
        return mDisplayName;
    }
public void setmDisplayName(String mDisplayName) {
    this.mDisplayName = mDisplayName;
}
}

firebase,

FirebaseUserDetails firebaseUserDetails = new FirebaseUserDetails();
firebaseUserDetails.setmDisplayName("arunwin");
DatabaseReference pathReference = FirebaseDatabase.getInstance().getReference().child("contacts");
pathReference.child("arun").setValue(firebaseUserDetails).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {

    }
});

, ,

contacts:
    arun:
        mDisplayName:"arunwin"
0

, . :

firebase:

{
"rules": {
".read": true,
".write": true
}
}

( email/pass)

createUserWithEmailAndPassword(email, password)

:

signInWithEmailAndPassword(email, password)

and you need to enable login via email and pass in console.

And then you can write data to your database.

0
source

In my case, I add a new baby like this!

NOTE: Here I add a new child refresh_tokento myfirebase database

FirebaseDatabase.getInstance().getReference().child("RegistrationModel").child(userId)
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Map<String, String> stringStringHashMap =(Map<String, String>) dataSnapshot.getValue();

                    stringStringHashMap.put("refresh_token",refreshedToken);

                    FirebaseDatabase.getInstance().getReference().child("RegistrationModel").child(userId)
                            .setValue(stringStringHashMap);

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
0
source

Source: https://habr.com/ru/post/1657581/


All Articles