1

I am developing an android application where older kids can pick up younger kids and walk to school. With the application the authenticated (email and password) younger kid can choose between three adresses to get picked up. As of right now my realtime database looks like this:

Database as of now

Should I make a new node named "Adresses" and have a structure like this below?

  Adresses
    Sherman Street
     username: Jannie

Because I want to retrieve the name of the street and all the users that have chosen the adress in a listview

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
JDoe
  • 221
  • 4
  • 14

1 Answers1

0

Your suggested method is good practice: you should try to flatten your data structure as much as possible.

I'd suggest using the user's ID for the membership of each address so it's easy to identify though. This way you can obtain a list of the members of "Sherman Street" from /addresses/Sherman Street and then match the keys listed within there to the users at /users/ with ease.

{
  "users": {
    "oXrJPVZsnMP3VKp9palSBfdnntk1": { ... },
    "xQDx3ntavhV3c02KFRPS8lxYfC62": { ... }
  },

  "addresses": {
    "Sherman Street": {
      "members": {
        "xQDx3ntavhV3c02KFRPS8lxYfC62": true
      }
    },
    "Wallaby Way": {
      "members": {
        "oXrJPVZsnMP3VKp9palSBfdnntk1": true
      }
    }
  }
}

You can also add backwards linking too by adding an address field to the user which matches the address name:

{
  "users": {
    "oXrJPVZsnMP3VKp9palSBfdnntk1": { ... },
    "xQDx3ntavhV3c02KFRPS8lxYfC62": {
      "username": "Jannie",
      "address": "Sherman Street"
    }
  },

  "addresses": {
    "Sherman Street": { ... }
  }
}

Using both together makes it easy to identify what users have selected which addresses, irrespective of which object you are currently handling within the app.

See the Firebase documentation on database structure for further details on structuring your data like this.

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • I like your first solution, as I want to add more properties to the "Sherman Street" child like "time to get picked up", so i'll try that one. Thanks for the answer – JDoe Oct 12 '17 at 15:23
  • Thanks for accepting, glad I could help. If you want, you can use both methods together, but it will mean updating `/users/$uid/address` and the list of members of the address under `/addresses/$name/members` when a user changes their address selection. This is helpful to link both objects in either direction though (user to address and address to users). The docs have a good example of this: [Update specific fields](https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields) (see the `writeNewPost()` method). – Grimthorr Oct 12 '17 at 15:35
  • Hey, thanks again for the help yesterday. I posted a new question regarding the structure of the database. If you have the time you are welcome to look at the question. https://stackoverflow.com/questions/46732732/android-retrieving-data-from-firebase – JDoe Oct 13 '17 at 15:16