august alsina
august alsina

Reputation: 197

Create database firebase

I am trying to create a firebase database separate for each user.

I have seen that I need to declare:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(**REF**);

What is the REF though?

Upvotes: 1

Views: 2102

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

A DatabaseReference is a local reference (think: a pointer) to a location in your database.

When you run this:

DatabaseReference rootReference = database.getReference();

You get a reference to the root of your database:

/ <root>

From that root you can get references to child nodes in your database:

DatabaseReference usersRef = rootReference.child("users");

Now usersRef points to:

/ <root>
    users

You can even specify a complete path when calling child() to get a pointer to anywhere in your database/tree:

DatabaseReference myProfilePicRef = rootRef.child("users/myuid/avatarURL");

And myProfilePicRef will refer to this URL

/ <root>
    users
        myuid
            avatarURL: "https://www.gravatar.com/avatar/12d378e6a9788ab9c94bbafe242b82b4?s=48&d=identicon&r=PG"

Finally to get to the code in your question: you can specify the path that you want to refer to straight away when you call get getReference. So these will result in reference to the same locations as before:

DatabaseReference rootRef = database.getReference();
DatabaseReference usersRef = database.getReference("users");
DatabaseReference myProfilePicRefRef = database.getReference("users/myuid/avatarURL");

Upvotes: 0

skynet
skynet

Reputation: 726

By default, database.getReference() gives you the default DatabaseReference of the default path.

database.getReference(**REF**) gets a DatabaseReference for the provided path.

Acc. to Firebase Official Docs

enter image description here

Upvotes: 1

Related Questions