android - 在 Firebase 中,我可以将一个用户连接到多个表吗?

标签 android firebase firebase-realtime-database

我的 Firebase 数据库中有两个表。在 flighthistory 中,我记录了飞机的飞行记录。在 flightperformance 中,我记录了每架飞机的性能。

我认为应该有第三个表,其中包含飞机的名称和 ID,并且应该在 flighthistoryflightperformance 中为飞机分配 ID。

这是考虑协调两个数据表的正确方法吗?我如何使用 Firebase 对此进行编码?

最佳答案

如果我没理解错的话,你需要三个表: 1:平面名称和 ID。 2:飞行历史。 3:飞行性能。

而不是将表 1 中的平面 ID 作为表 2 和表 3 中的外键(就像在 sql 数据库中一样),您想要在 firebase 中使用相同的“解决方案”吗?

如果是这样: 创建一个包含所有平面的平面表。当您创建一个新平面时,您将获得的 key (通过使用 getkey())将是该特定平面在 firebase 中的 ID。您可以使用相同的 key 作为 Flighthistory 和 Flightperformance 中的引用,将飞机与历史和性能表联系起来。

Firebase“表格”将是这样的:

  • 平面 -> 每个平面的 ID/键 -> 平面名称和型号(也许?)。

  • FlightHistory -> 您可以在此处为飞机使用相同的 ID/ key -> 一些飞行历史数据。一个好的解决方案是使用某种自动增量(或者日期 + 时间?),并且在这个数字中,会有飞机来自哪里和目的地的数据。

  • 飞行性能 -> 每架飞机的 ID 相同 -> 一些飞行性能数据。

编辑:如何做到这一点的一个小例子:

//Get the firebase database reference in onCreate(). For this example, mDatabase will be the databasereference

public void createPlane() {
    //First getting the key which will be used as the reference between the tables
    String key = mDatabase.child("Planes").push().getKey();

    //Create a new plane
    Plane plane = new Plane("Coolplane", "Boeing 777-400");

    //Adding the plane to firebase, with the key as the reference
    mDatabase.child("Planes").child(key).setValue(plane);

    //Starting the methods that create the flighthistory and performance.Passing the key, which will be used as the ID in firebase (which connects the plane with the history and performance
    newFlightHistory(key);
    newPerformance(key);

//Not sure how your history and/or performance table will look like, but you 
just pass the key to these methods, and use these keys as the key when you 
create a new history / performance. 

}

注意:由于 Firebase 是基于 JSON 的数据库,因此它通常不是构建数据的“正确”方式。这完全取决于您要使用什么查询,以及您将如何使用数据。

关于android - 在 Firebase 中,我可以将一个用户连接到多个表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804955/

相关文章:

java - API 级别较低的设备上的 Android UnkownHostException

c# - 在 Xamarin.Forms 中,在我将 ScrollView 添加到页面后,我所有的点击事件都停止工作。 ScrollView 是否拦截点击?

android - 如何从 Android Studio 的 UI 在 build.gradle 文件中添加新的依赖项

java - 从动态 Firebase 数据库中提取变量

android - 使用android数据绑定(bind)将数据从Recycler View onClick传递到Detail Activity

android - 单击后获取动态创建的 subview 的位置

android - 从抽屉导航 Activity 返回会使应用程序崩溃

firebase - Firebase Functions 上传入和传出 API 端点的数据是否已加密?

javascript - 使用基于 firebase 查询的规则来保护数据(使用 orderByChild 和不可索引的键)

android - 从 firebase 数据库读取数据失败