Android - 数据库结构(Firebase) [英] Android - Database Structure (Firebase)

查看:142
本文介绍了Android - 数据库结构(Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我有三个usertypes:学校,老年学生,和年轻的孩子。学校在地图上制作不同的路线,并指定年龄较大的学生负责该路线。学校还在路线上放置多个站点。这些站是老年学生拿起小孩的地方。

在我的应用程序中,学校usertype可以在地图上设置两个标记,路径将被绘制两个标记之间(路线)。我想保存这两个标记的LatLng,因为它们表示路线的开始和结束。我也想保存两个标记的地址。我使用地理编码器来获取路线的地址。



学校一次只能添加一个标记(停止)。当标记被放置时,用户被提示输入标记的地址以及年长的学生应该拿起年幼的孩子的时间。我想保存标记的LatLng,地址和输入的时间。

学校应该能够看到在列表视图中创建的路径,其中起始地址,结束地址,带时间的停止地址显示。当在列表视图中点击路线时,应该显示一个所有年龄较大的学生的名单,学校可以指定年龄较大的学生负责路线。



能够看到他们分配的路线和停车位。



年幼的孩子应该只能看到停止。通过点击地图上的标记,他们表示他们希望在那个站点被拿起。



截至目前,我的firebase数据库的结构是:

 学校名称
使用者
使用者ID
使用者名称
使用类型
记号
uniqueID
纬度
经度
地址
时间
路线
uniqueID
startAddress
endAddress
地点
0
纬度
经度
1
纬度
经度

其中位置节点是路线的航点数组。这种结构使我很难满足我的要求。任何更好的数据库结构的想法?

$ pre $公共类Route {

私有ArrayList< MapFragment.Location> ;位置;

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

$ b public Route(){
}

public Route(ArrayList< MapFragment.Location> locations,String startDestination,String endDestination,HashMap< String ,布尔> 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(){
返回位置;


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





$ b

解决方案

听起来像这些对象之间的关系可以概括为:





我们知道,Firebase实时数据库是一个NoSQL数据库,因此不明确支持关系,但可以推断在这个结构中,例如这个结构(其中 ... 是你现有的值):

<$ p $ schoolId
用户
userId
...
标记
markerId
...
route //此标记所属的路线ID
路线
routeId
...
标记//属于此路线的标记ID列表



$ c> users >关系,只需处理标记 routes

有了这个结构:我们目前是使用一个标记还是一个路径,我们可以很容易地找出它们是如何相互关联的其他。

通过 route 值执行查询

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

其中 routeId 是我们目前正在使用的路线。



创建对象时需要注意一些变化,例如,当您使用此结构创建标记时,您需要:


  • 将标记的路径值设置为它所属的路线的ID和
  • 将标记ID添加到它所属的路线下的标记列表中。



您可以使用 transaction 多位置更新


strong>:你实际上不必使用 routeId / markers 列表,但如果您使用 FirebaseUI显示索引数据

这种使用ID作为索引键的技术称为数据扇出,您可以在构建您的数据库文档

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.

这篇关于Android - 数据库结构(Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆