从两个Firebase数据库表中获取所有数据

huangapple go评论62阅读模式
英文:

Get all data from two Firebase database tables

问题

我在 Firebase 数据库中有两个表。

表 A:		表 B:

a				a  2 张图片
b				b  3 张图片
c
d				d  1 张图片
e

结果显示为:

a  2 张图片
b  3 张图片
d  1 张图片

我需要的结果是:

a  2 张图片
b  3 张图片
c  0 张图片
d  1 张图片
e  0 张图片

我不知道如何编写查询。

我编写的查询是,如果表 A 中的 a 等于表 B 中的 a,那么 a 将显示为 2 张图片。这样,显示了 a、b 和 d。但是 c 和 e 不显示,因为在表 B 中找不到它们。我希望显示 c 和 e,但没有图片

我尝试过的内容:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list = new ArrayList<ListType>();
        list.clear();
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            ListType listType = ds.getValue(ListType.class);
            final String listname = listType.getName();
            
            databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        if (postSnapshot.getKey().equals(listname)) {
                            String count = String.valueOf(postSnapshot.getChildrenCount());
                            ListType listTypee = new ListType(listname, count);
                            list.add(listTypee);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }

希望这能帮助你解决问题。如果需要进一步的帮助,请告诉我。

英文:

I have two tables in firebase database.

Table A :		Table B:

a				a  2 pic
b				b  3 pic
c
d				d  1 pic
e

Result shows as :

a  2 pic
b  3 pic
d  1 pic

I need result as :

a  2 pic
b  3 pic
c  0 pic
d  1 pic
e  0 pic

I am unable How I will write the query.

I write the query as if a from Table A, equals a in Table B, then a will show with 2 pic. In this way, a,b and d shows. But c and e are not showing as c and e not equal in Table B. I want to show c and e with 0 pictures.

What I have tried:

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list = new ArrayList&lt;ListType&gt;();
            list.clear();
            for(DataSnapshot ds:dataSnapshot.getChildren()) {
                ListType listType = ds.getValue(ListType.class);
                final String listname = listType.getName();
				
                databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
                           if (postSnapshot.getKey().equals(listname)) {
                                String count = String.valueOf(postSnapshot.getChildrenCount());
                                ListType listTypee = new ListType(listname, count);
                                list.add(listTypee);
                                adapter.notifyDataSetChanged();
                           }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }

答案1

得分: 1

你现在正在循环遍历表B中的所有节点,并将它们的计数添加到列表中。但由于C和E不在表B中,它们永远不会出现在列表中。

我实际上会颠倒逻辑,并:

  1. 循环遍历读取表A的结果
  2. 检查表B中是否有这些节点
  3. 添加节点数或0

在代码中可能如下所示:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list = new ArrayList<ListType>();
        list.clear();
        for(DataSnapshot ds: dataSnapshot.getChildren()) {
            ListType listType = ds.getValue(ListType.class);
            final String listname = listType.getName();
            
            databaseReference1.child(listname).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot bSnapshot) {
                    String count = String.valueOf(bSnapshot.getChildrenCount());
                    ListType listTypee = new ListType(listname, count);
                    list.add(listTypee);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException(); // 永远不要忽略错误
                }
            });
        }
    }
});

请注意,循环内的各个监听器并不像你可能想象的那样慢,因为Firebase 对请求进行了流水线处理

英文:

You're now looping over all nodes in table B, and adding their counts to the list. But since C and E are not in table B, they'll never show up in the list.

I'd actually invert the logic, and:

  1. Loop over the results of reading table A
  2. Check if there are nodes for that in table B
  3. Add either the number of nodes or 0

In code that'd be something like:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list = new ArrayList&lt;ListType&gt;();
        list.clear();
        for(DataSnapshot ds: dataSnapshot.getChildren()) {
            ListType listType = ds.getValue(ListType.class);
            final String listname = listType.getName();
            
            databaseReference1.child(listname).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot bSnapshot) {
                    String count = String.valueOf(bSnapshot.getChildrenCount());
                    ListType listTypee = new ListType(listname, count);
                    list.add(listTypee);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException(); // never ignore errors
                }
            });
        }

Note that the individual listeners inside the loop are not nearly as slow as you may think, since Firebase pipelines the requests.

huangapple
  • 本文由 发表于 2020年8月5日 01:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63251967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定