如何在返回语句之前从FirebaseFirestore获取所有数据

时间:2019-09-05 20:24:44

标签: java android firebase android-asynctask google-cloud-firestore

我需要一个函数来从FirebaseFirestore获取所有数据/注释。 如何使此函数在返回之前等待所有数据?

我认为我创建的此函数在主线程中不起作用 然后从Firebase获取数据之前先返回

    public static ArrayList<NoteFB> getNotes() {

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        final String TAG = "FB Adapter";

        final ArrayList<NoteFB> doFBs = new ArrayList<>();
        db.collection("notesItem")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                doFBs.add(document.toObject(NoteFB.class));
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                    }
                });

        return doFBs;
    }

        MyFBAdapter myFBAdapter = new MyFBAdapter(ShowActivity.this, FBAdapter.getNotes());

        rvContacts.setAdapter(myFBAdapter);

此代码返回一个空的ArrayList,该列表创建一个空的recyclerview。

2 个答案:

答案 0 :(得分:0)

最近出现了很多这些问题。我已经找到解决方案了:使用Tasks API。

> result: 
{
  "$undefined": true
}
> result (JavaScript): 
EJSON.parse('{"$undefined":true}')

如果我遇到语法错误,请原谅我的Java有点生锈。

确保您在主线程上关闭了此代码,否则它将崩溃。

答案 1 :(得分:0)

您可以为此使用界面

public interface NoteDataInterface {
        void onCompleted(ArrayList<NoteFB> listNotes);
    }

更改方法以使用界面:

 public static void getNotes(NoteDataInterface noteDataInterface) {

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        final String TAG = "FB Adapter";

        final ArrayList<NoteFB> doFBs = new ArrayList<>();
        db.collection("notesItem")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                doFBs.add(document.toObject(NoteFB.class));
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                       noteDataInterface.onCompleted(doFBs);
                    }
                });


    }

然后调用您的方法:

 getNoteData(new NoteDataInterface() {
            @Override
            public void onCompleted(ArrayList<NoteFB> listNotes) {
                Log.e("listNotes>>",listNotes.size()+"");
            }
        });