1

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?

1
  • 1
    One database per user is not really a good design. You can limit data a user "has access to" by appending the user ID to each related record, then you can filter by that Commented Oct 16, 2016 at 6:13

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.