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

how get all data from FirebaseFirestore before return statement

我需要一个函数来从 FirebaseFirestore 获取所有 data/notes。 如何让此函数在 return 之前等待所有数据?

我认为我创建的这个函数在主线程中不起作用 return 从 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);

此代码return是一个创建空回收视图的空 ArrayList。

最近出现了很多这样的问题。我不久前找到了解决方案:使用任务 API.

public static ArrayList<NoteFB> getNotes() {

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

    final ArrayList<NoteFB> doFBs = new ArrayList<>();

    try {
        Task<QuerySnapshot> taskResult = Tasks.await(db.collection("notesItem").get(), 2, TimeUnit.SECONDS)
        for (QueryDocumentSnapshot document : task.getResult()) {
            Log.d(TAG, document.getId() + " => " + document.getData());
            doFBs.add(document.toObject(NoteFB.class));
         }

    } catch(Exception e) {
         Log.w(TAG, "Error getting documents.", e.localizedString());
    }
    return doFBs
}

如果我犯了任何语法错误,请原谅我,我的 Java 有点生疏。

确保您在主线程之外调用此代码,否则它会崩溃。

您可以为此使用界面

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()+"");
            }
        });