猿问

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

我需要一个函数来从 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。


缥缈止盈
浏览 111回答 2
2回答

叮当猫咪

最近出现了很多这样的问题。我不久前就找到了解决方案:使用任务 API。public static ArrayList<NoteFB> getNotes() {&nbsp; &nbsp; FirebaseFirestore db = FirebaseFirestore.getInstance();&nbsp; &nbsp; final String TAG = "FB Adapter";&nbsp; &nbsp; final ArrayList<NoteFB> doFBs = new ArrayList<>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Task<QuerySnapshot> taskResult = Tasks.await(db.collection("notesItem").get(), 2, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; for (QueryDocumentSnapshot document : task.getResult()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, document.getId() + " => " + document.getData());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doFBs.add(document.toObject(NoteFB.class));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.w(TAG, "Error getting documents.", e.localizedString());&nbsp; &nbsp; }&nbsp; &nbsp; return doFBs}如果我犯了任何语法错误,请原谅我,我的 Java 有点生疏了。确保您在主线程之外调用此代码,否则它将崩溃。

烙印99

您可以为此使用接口public interface NoteDataInterface {&nbsp; &nbsp; &nbsp; &nbsp; void onCompleted(ArrayList<NoteFB> listNotes);&nbsp; &nbsp; }更改您的方法以使用接口:&nbsp;public static void getNotes(NoteDataInterface noteDataInterface) {&nbsp; &nbsp; &nbsp; &nbsp; FirebaseFirestore db = FirebaseFirestore.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; final String TAG = "FB Adapter";&nbsp; &nbsp; &nbsp; &nbsp; final ArrayList<NoteFB> doFBs = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; db.collection("notesItem")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<QuerySnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (QueryDocumentSnapshot document : task.getResult()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, document.getId() + " => " + document.getData());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doFBs.add(document.toObject(NoteFB.class));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.w(TAG, "Error getting documents.", task.getException());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;noteDataInterface.onCompleted(doFBs);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }然后调用你的方法:&nbsp;getNoteData(new NoteDataInterface() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCompleted(ArrayList<NoteFB> listNotes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("listNotes>>",listNotes.size()+"");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答