简体   繁体   中英

Android - Database Structure (Firebase)

I have an application where I have three usertypes: "The school", "Older Students", and "Younger kids". The school makes different routes on a map and assigns the older students as responsible for that route. The school also places multiple stops on the route. The stops are places where the older students pick up the younger kids.

In my application the school usertype is able to set two markers on the map and a path will be drawn between the two markers ( route ). I want to save the LatLng of both markers as they indicate the start and end of the route. I also want to save the address of both markers. I use geocoder to get the address of the routes.

The school is also able to add just one marker at a time( stops ). When the marker is placed the user is prompted to enter the address of the marker and the time of which the older students should pick up the younger kids. I want to save the LatLng, address of the marker and the entered time.

The school should be able to see the routes created in a listview where the start-address, end-address, stops-addresses with the time is displayed. When clicking on the routes in the listview there should be shown a list of all older students where the school can assign the older students as responsible for the route.

The older students be able to see the route and the stops they are assigned to.

The younger kids should be able to see just the stops . By clicking on the marker on the map they indicate that they want to get picked up at that stop.

As of now the structure of my firebase database is:

School Name
   users
     userID
       username 
       usertype
   markers
     uniqueID
       latitude
       longitude
       address
       time
   routes
     uniqueID
        startAddress
        endAddress
        locations
           0
             latitude
             longitude
           1
             latitude
             longitude

Where the locations node is an array of waypoints of the route. This structure makes it very difficult to meet my requirements. Any ideas of a better database structure?

public class Route {

private ArrayList<MapFragment.Location> locations;

private String startDestination;
private String endDestination;
HashMap<String, Boolean> markerId;


public Route() {
}

public Route(ArrayList<MapFragment.Location> locations, String startDestination, String endDestination, HashMap<String, Boolean> markerId) {
    this.locations = locations;
    this.startDestination = startDestination;
    this.endDestination = endDestination;
    this.markerId = markerId;
}

public String getStartDestination() {
    return startDestination;
}

public void setStartDestination(String startDestination) {
    this.startDestination = startDestination;
}

public String getEndDestination() {
    return endDestination;
}

public void setEndDestination(String endDestination) {
    this.endDestination = endDestination;
}


public ArrayList<MapFragment.Location> getLocations() {
    return locations;
}

public void setLocations(ArrayList<MapFragment.Location> locations) {
    this.locations = locations;
}


}

在此处输入图片说明

It sounds like the relationships between these objects could be summarized as:

As we know, the Firebase Realtime Database is a NoSQL database and therefore doesn't explicitly support relationships, but they can be inferred in the structure, so take this structure for example (where ... is your existing values):

schoolId
  users
    userId
      ...
  markers
    markerId
      ...
      route      // the route ID that this marker belongs to
  routes
    routeId
      ...
      markers    // list of marker IDs that belong to this route

Note : To keep this example simple, I'll ignore the users relationships for now and just work with the relationships between markers and routes .

With this structure: whether we are currently working with a marker or a route, we can easily find out how they relate to each other.

It is then easy to get all markers related to a specific route by performing a query against the route value of children in the markers node:

schoolReference.child("markers").orderByChild("route").equalTo(routeId);

Where routeId is the unique ID of the route we are currently working with.

You'll need to be aware of some changes when creating objects though, so for example, when you create a marker with this structure, you'll need to:

  • Set the route value of the marker to the the ID of the route that it belongs to, and
  • add the marker ID to the markers list under the route that it belongs to.

You can achieve this in one write operation using a transaction or a multi-location update .

Note : You don't actually have to use the routeId/markers list, but it can be useful if you're using FirebaseUI to display indexed data .

This technique of using IDs as index keys is called data fan out and you can read more about it in the structure your database documentation .

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