Android-从Firebase查询数据 [英] Android - Querying data from Firebase

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

问题描述

我的Firebase实时数据库中有要查询的数据并在Recyclerview中显示,但是我对如何执行此操作感到困惑.

数据库结构:

对于一项活动,我只想查询标记节点. 使用FirebaseUI似乎很容易

对于另一项活动,我想查询所有路线和所有标记.因此,我的回收站视图中的每一行都由组成:一条路线的startDestination和endDestination,连接到该路线的标记的地址和时间

解决方案

这是我以前的回答的一个后续内容在这里此处,因此,我会尽力为您提供一些示例.

创建可扩展的数据

这里的基本前提是,在显示路线列表时,您要尝试避免下载与路线标记关联的所有数据.这是因为此时您不需要了解有关标记的所有信息,只需了解部分细节即可.

此外,从数据库(通过网络)下载以显示路线列表的数据越少,该列表的组成速度就越快.举例来说,假设您有1000条路线,每条路线有10个标记;这意味着仅下载1000条路线和10000条标记即可显示路线列表.

这就是为什么文档建议使用数据扇出的原因创建可扩展的数据,并且还建议取消规范化并在NoSQL数据库中复制数据.

数据重复

因此,为了快速轻松地获得与特定路线相关的标记的可用列表,您将需要像在标记ID上所做的那样开始在路径节点上存储标记地址和时间.

对此的一种可能的解决方案是创建一个附加的PartialMarker类,该类保存有关在路由列表中使用的标记的有限信息:

public class PartialMarker {
    private String address;
    private String time;

    public PartialMarker() {}

    public PartialMarker(String address, String time) {
        this.address = address;
        this.time = time;
    }

    public String getAddress() { return address; }

    public String getTime() { return time; }
}

,然后在您的Route类下有一个Map:

public Route {
    // ...

    private HashMap<String, PartialMarker> partialMarkers = new HashMap<>();

    public HashMap<String, PartialMarker> getPartialMarkers() { return partialMarkers; }
}

因此,当您将新标记保存到路由的数据库时,您还将为每个标记创建一个PartialMarker实例并将其保存到路由节点,例如:

public void saveToDatabase() {
    // Do your thing to save the route

    // Create some partial markers
    HashMap<String, Object> partialMarkers = new HashMap<>();
    for (Map.Entry<String, Object> entry : markers.entrySet()) {
        String key = entry.getKey();
        FirebaseMarker marker = (FirebaseMarker) entry.getValue();
        partialMarkers.put(key, new PartialMarker(marker.getAddress(), marker.getTime()));
    }

    // Store the partial markers under the route
    schoolReference.child("routes").child(routeId).child("partialMarkers").updateChildren(partialMarkers);
}

然后,当将Route绑定到RecyclerView中的ViewHolder时,可以遍历每个PartialMarker以在该行上显示它们的列表:

for (PartialMarker partialMarker : model.getPartialMarkers().values()) {
    // ...
}

结论

您可以通过许多其他方法来执行此操作,但是我希望这可以使您深入了解使用非规范化数据来实现此目标的可能方法.一开始很难理解,因为数据的复制似乎是向后的,但这是NoSQL数据库的建议模式,当您开始处理大量数据时,这是有道理的.

I have data in my Firebase realtime database that I want to query and show in my Recyclerview, but I am confused on how to do this.

Database Structure:

For one activity I want to query only the markers node. This seems easy to do with FirebaseUI

For another activity I want to query all routes and all markers. So each row in my recyclerview consist of: startDestination and endDestination of one route, the addresses and times of the markers connected to that route

解决方案

This is somewhat a follow on from my previous answers here and here, so I'll do my best to provide some examples for you.

Create data that scales

The basic premise here is that you want to try to avoid downloading all the data associated with the route's markers when displaying a list of routes. This is because you won't need to know everything about the markers at this point, only partial details.

Furthermore, the less data you download from the database (across the network) to display a list of routes, the quicker this list can be composed. Imagine, for instance, that you have 1000 routes and each route has 10 markers; this would mean downloading 1000 routes and 10000 markers just to display a list of routes.

This is why the documentation suggests using data fanout to create data that scales and it's also recommended to denormalize and duplicate data in NoSQL databases.

Data duplication

So, in order to quickly and easily obtain a useable list of markers associated with a specific route, you'll want to start storing marker addresses and times on the route node, like you have done with marker IDs.

One possible solution to this would be to create an additional PartialMarker class that holds limited information about a marker for use on the route list:

public class PartialMarker {
    private String address;
    private String time;

    public PartialMarker() {}

    public PartialMarker(String address, String time) {
        this.address = address;
        this.time = time;
    }

    public String getAddress() { return address; }

    public String getTime() { return time; }
}

And then have a Map of these under your Route class:

public Route {
    // ...

    private HashMap<String, PartialMarker> partialMarkers = new HashMap<>();

    public HashMap<String, PartialMarker> getPartialMarkers() { return partialMarkers; }
}

So that when you save a new marker to the database for a route, you'll also create a PartialMarker instance for each marker and save this to the route node, something like:

public void saveToDatabase() {
    // Do your thing to save the route

    // Create some partial markers
    HashMap<String, Object> partialMarkers = new HashMap<>();
    for (Map.Entry<String, Object> entry : markers.entrySet()) {
        String key = entry.getKey();
        FirebaseMarker marker = (FirebaseMarker) entry.getValue();
        partialMarkers.put(key, new PartialMarker(marker.getAddress(), marker.getTime()));
    }

    // Store the partial markers under the route
    schoolReference.child("routes").child(routeId).child("partialMarkers").updateChildren(partialMarkers);
}

Then, when you bind your Route to a ViewHolder in the RecyclerView, you can iterate over each PartialMarker to show a list of them on that row:

for (PartialMarker partialMarker : model.getPartialMarkers().values()) {
    // ...
}

Conclusion

You could do this any number of other ways, but I hope this gives you an insight into a possible method to achieve this with denormalized data. It's difficult to understand at first because duplicating of the data seems backwards, but this is a suggested pattern for NoSQL databases, and it makes sense when you start working with huge amounts of data.

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

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