简体   繁体   中英

Android - Firebase Database Structure

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:

截至目前的数据库

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM