等待Firebase异步在Android中检索数据 [英] Wait Firebase async retrieve data in Android

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

问题描述

我需要存储由他自己异步生成的FireBase getValue方法的结果.我不能使用诸如"onPostExecute()"之类的东西,并且出于我的目的,我无法执行所有的操作"into onDataChange()",因为在将来的其他活动中我需要一些引用.

I need to store the result of FireBase getValue method that is async by his own. I can't use something like "onPostExecute()" and, for my purpose, i can't execute all my operation "into onDataChange()" because i need some references in future time in other activities.

这是我的代码段以检索数据:

Here my snippet to retrieve data:

    List<Village> villages = new LinkedList<>();
    Firebase ref = new Firebase("MYFIREBASEURL").child("village");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            spinnerArray.clear();
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Village v = postSnapshot.getValue(Village.class);
                villages.add(v);
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

如果我尝试从"onDataChange"中读取村庄,那么对于他的异步生活,我自然会得到null值.有一种方法可以确保调用onDataChange?.

If i try to read villages out of "onDataChange" i have, naturally for his async life, null value. There is a way to ensure that onDataChange was called?.

推荐答案

抱歉,这不是您想要的,因为我的英语太差了. 您可以定义一个侦听器接口来处理它. 示例:

Sorry if it's not what you want because my English too bad. You can define a listener interface to handle it. Example:

 public interface OnGetDataListener {
    public void onStart();
    public void onSuccess(DataSnapshot data);
    public void onFailed(DatabaseError databaseError);
}

然后我创建了一个自定义函数来获取Database.class中的数据:

And I create a custom function to get data in Database.class:

  public void mReadDataOnce(String child, final OnGetDataListener listener) {
    listener.onStart();
   FirebaseDatabase.getInstance().getReference().child(child).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listener.onSuccess(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            listener.onFailed(databaseError);
        }
    });
}

当我想从Main.class获取数据时,我做了:

When I want to get data from Main.class, I do:

private void mCheckInforInServer(String child) {
    new Database().mReadDataOnce(child, new OnGetDataListener() {
        @Override
        public void onStart() {
            //DO SOME THING WHEN START GET DATA HERE
        }

        @Override
        public void onSuccess(DataSnapshot data) {
           //DO SOME THING WHEN GET DATA SUCCESS HERE
        }

        @Override
        public void onFailed(DatabaseError databaseError) {
           //DO SOME THING WHEN GET DATA FAILED HERE
        }
    });

}

通过这种方式,您可以重用方法并处理数据.

By this way, you can reuse your method and handle data.

这篇关于等待Firebase异步在Android中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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