从Firebase检索数据

从Firebase检索数据

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

问题描述

我正在开发一个android应用程序,大孩子可以接小孩子并去上学.通过该应用程序,经过身份验证(电子邮件和密码)的小孩可以在三种地址之间进行选择.截至目前,我的实时数据库如下所示:

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:

我想检索不同的地址以及选择地址的用户.我想我必须使用recyclerview来获取数据,但是我不确定是否可以使用我的数据库结构.

I want to retrieve the different addresses and the users who picked the addresses. I am thinking I have to use recyclerview to get the data, but I am unsure on if it is possible to do with my database structure.

推荐答案

接着是我之前的答案,应该满足您的要求,以创建一个包含所有地址及其关联用户的列表,该列表可能更接近您要查找的内容.

Following on from my previous answer, this one should accommodate your requirement to create a list of all addresses and their associated users, which may be closer to what you're looking for.

同样,您可以使用 FirebaseUI数据库包简化RecyclerView的创建.

Again you can use the FirebaseUI database package to simplify the RecyclerView creation.

您需要开始对数据进行非规范化,因此您的数据结构还应在地址节点中包括用户名:

You'll need to start denormalizing your data, so your data structure should also include usernames in the addresses node:

{
  "Addresses" : {
    "Street 10" : {
      "name" : "Street 10",
      "users" : {
        "VAzdMWafK6cyhmJnOI4br5xiQg93" : "John"
      }
    }
  },
  "User" : {
    "VAzdMWafK6cyhmJnOI4br5xiQg93" : {
      "username" : "John",
      "address" : "Street 10"
    }
  }
}

注意:您只需要将用户ID添加到其选择的地址(如果更改选择,则删除该节点),因此请勿对未选择用户的地址使用"VAzdMWafK6cyhmJnOI4br5xiQg93" : false,因为这可能会引起混乱

Note: you only need to add user IDs to their chosen address (and remove the node if they change selection), so don't use "VAzdMWafK6cyhmJnOI4br5xiQg93" : false for addresses the user has not selected as this could cause confusion.

然后您可以使用:

Query query = FirebaseDatabase.getInstance().getReference("/Addresses");

FirebaseRecyclerOptions<Address> options = new FirebaseRecyclerOptions.Builder<Address>()
        .setQuery(query, Address.class)
        .build();

Address类似于:

public class Address {
    private String name;
    private Map<String, String> users;

    public Address() {}

    public Map<String, String> getUsers() {
        return this.users;
    }

    // ...
}

然后创建options变量中的>实例.然后,在将适配器绑定到Viewholder时,您可以访问users映射以列出选择了该地址的每个用户,而无需不必要地加载整个User对象.

And create a FirebaseRecyclerAdapter instance from the options variable. Then when binding the viewholder in the adapter, you can access the users map to list each user that has selected this address, without the need to load the entire User object unnecessarily.

此模式称为非规范化,是使用NoSQL数据库(如Firebase)时的建议方法实时数据库).这样做的主要缺点是数据重复:因此,例如,当用户更改所选地址时,您需要进行以下更改:

This pattern is called denormalization and is the suggested approach when using NoSQL databases (like Firebase Realtime Database). The main downside to this is data duplication: so for example, when a user changes their selected address, you'll need to change:

  • 用户下的address值,和
  • 地址下的users列表.
  • The address value under the user, and
  • the users list under the address.

同样,如果允许用户更改用户名,则需要在用户选择的地址下以及用户节点中更新用户名.

Likewise, if a user is allowed to change their username, you'll need to update the username under their chosen address as well as in the user's node.

有关处理此问题的详细信息,请参见此答案,其中解释了许多方法(尽管示例在JavaScript中,前提仍然适用).

For details on dealing with this, see this answer which explains a number of methods (although the examples are in JavaScript, the premise still applies).

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

09-02 20:07