【问题标题】:How to query relations in Firebase in Android apps如何在 Android 应用中查询 Firebase 中的关系
【发布时间】:2017-02-02 17:13:46
【问题描述】:

假设我有一个用户拥有宠物的应用。

根据我在不同来源中阅读的内容,这是在 Firebase DB 中建模关系的有效方法:

{
  "users":{
    "user1":{
      "name":"Arthur",
      "pets":{
          "pet1":true,
          "pet2":true
      }
    },
    "user2":{
      "name":"George",
      "pets":{
        "pet3":true,
        "pet4":true
      }
    }
  },
  "pets":{
    "pet1":{...},
    "pet2":{...},
    "pet3":{...},
    "pet4":{...}
  }
}

假设我有一个 Android 应用程序,我想显示所有用户的列表,并在每个列表项中还提及该用户拥有哪些宠物。

我想要的是一个用户对象列表,其中每个用户对象都有宠物对象列表。例如:

public class User {

    public String name;
    public List<Pet> pets;

    ...
}

查询数据库以获得此结果的最佳方法是什么?

【问题讨论】:

  • 有什么问题/担忧?因为它似乎是loading users from a listjoining in the pets(或one of these)的组合。如果您担心连接性能,请阅读my answer here
  • 感谢@FrankvanPuffelen,我担心的是:我想要的是用户对象列表(如问题所示)。比如说我有 100 个用户,这是否意味着我需要为用户进行查询,然后循环并为每个用户进行另一个查询(100 个查询)?然后我需要自己设置每个用户的宠物列表......这看起来很复杂和不方便
  • 如果要显示 100 个用户并使用索引数据结构,则需要获取 100 个项目。这是一个客户端join操作,确实会导致更多的客户端代码。但它并不像你想象的那么慢,正如我在第一条评论的最后一个链接中所解释的那样。
  • 知道了。我担心这就是答案:) 谢谢

标签: android firebase firebase-realtime-database


【解决方案1】:

考虑到您有 FirebaseDatabase 实例,您首先需要一个查询

databaseQuery = firebaseDatabase.child("users").child("user1").orderByChild("name");

如果您想了解更多关于如何在 Firebase 中执行常见 SQL 查询的信息,可以在此处阅读 Common SQL Queries Converted for Firebase

然后通过FirebaseUI,你可以按照你想要的方式显示数据。

为了将 firebase 对象映射到您的 POJO,我使用了 FirebaseUI

 adapter = new FirebaseRecyclerAdapter<POJOClass, CustomVH>(POJOClass.class, R.layout.your_layout_file, CustomVH.class, databaseQuery)
    {
        @Override
        protected void populateViewHolder(final CustomVH viewHolder,final POJOClass model, int position)
        {
            viewHolder.setListItemTitle(model.getItem());
            viewHolder.setListItemCategory(model.getCategory());
            viewHolder.setImageView(model.getDownloadImageUrl(),getContext());
        }
    };
    recyclerView.setAdapter(adapter);

我的 ViewHolder 类为:

public class CustomVH extends RecyclerView.ViewHolder {
private final TextView listItemTitle;
public CheckBox checkBox;
private final TextView listItemCategory;
private ImageView imageView;

public CustomVH(View itemView)
{
    super(itemView);
    listItemTitle = (TextView) itemView.findViewById(R.id.listItemText);
    checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxListItem);
    listItemCategory = (TextView) itemView.findViewById(R.id.listItemCategoryText);
    imageView = (ImageView) itemView.findViewById(R.id.customMyBucketListItemImage);
    deleteButton = (ImageButton) itemView.findViewById(R.id.listItemDeleteButton);
}

public void setListItemTitle(String name)
{
    listItemTitle.setText(name);
}

public void setListItemCategory(String category)
{listItemCategory.setText(category);}

public void setImageView(String imageUrl, Context context)
{
    //set your imageview
}
}

【讨论】:

  • 谢谢,但我不明白这如何回答我关于在 FB 中加入表格的问题
  • Firebase 中没有这样的表。 This 会有所帮助。
  • 我希望有一个可以在 FireBase 这边完成的操作,但我知道这是不可能的。无论如何,我的问题是关于 Android 的 FireBase 并以最简单的方式将响应转换为 Java POJO(不是您的答案所指的)。还是谢谢
  • @dors 请检查我的更新答案,FireBase with Android,并以最简单的方式将响应转换为 Java POJO
  • 非常感谢您为回答这个问题所做的努力,但请再次查看。我的问题不是指对特定对象的琐碎 Firebase 查询,然后将其呈现在 RecyclerView 中,而是指使用 Firebase 以最简单的方式执行类似于 SQL DB 的连接的操作。我现在明白了,在查询级别不可能进行“加入”,我需要执行几个查询来执行它,这就是我的问题的答案
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 2017-03-16
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多