聆听多个数据库引用 [英] Listen to multiple database references

查看:130
本文介绍了聆听多个数据库引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
A:[data-a],
B:[data-b],
C:[data-c]
}
A
> B C 我想要显示Android中 ListView

对于一个位置,我已经能够通过将下面的代码添加到我的项目中实现此目的。

  DatabaseReference databaseReference = FirebaseDatabase.getInstance()。getReference(A); 
databaseReference.addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
//添加的子元素
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
// Child got changed
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
// Child remove b $ b}
$ b $ @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){
// Child got move
}

@Override $ b $ public void onCancelled(DatabaseError databaseError){
//发生了一些错误
}
});

到我的问题。我是否需要为每个位置使用多个侦听器,或者可以以某种方式以更简单的方式得到有关所有更改的通知?

将需要添加多个引用和子监听器。 Firebase没有联盟,SQL数据库中的其他操作符的概念。您要检索的数据集合与用来访问它们的引用/侦听器之间存在1:1的关系。您不能使用通配符或其他机制一次访问多个数据集合。


I created a project in which I need to listen to multiple database locations at once.

{
  "A": [data-a],
  "B": [data-b],
  "C": [data-c]
}

Whenever anything gets changed, added or deleted at location A, B or C I want to display the action which happened inside a ListView in Android.

For one location I have been able to achieve this by adding the following code to my project.

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("A");
databaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    //Child got added
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    //Child got changed
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    //Child got removed
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    //Child got moved
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    //Some error happened
    }
});

Now to my question. Do I need multiple listeners for each location or can I somehow get notified about all changes in a simpler way?

解决方案

Yes, you would need to add multiple references and child listeners. Firebase has no concept of unions, joins, or other operators present in SQL databases. There is a 1:1 relationship between collections of data you want to retrieve and the references / listeners you use to access them. You cannot use wildcards or other mechanisms to access more than one data collection at a time from a single call.

这篇关于聆听多个数据库引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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