Android Firestore convert array of document references to List

后端 未结 1 1185
半阙折子戏
半阙折子戏 2020-11-27 23:07

In my Firestore I\'ve got a users collection, within which the documents could have a bookmarks field, which is an array of references:

相关标签:
1条回答
  • 2020-11-27 23:58

    Yes it is. Please see the following lines of code:

    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser != null) {
        String uid = firebaseUser.getUid();
        rootRef.collection("users").document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        List<DocumentReference> list = (List<DocumentReference>) document.get("bookmarks");
                        List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
                        for (DocumentReference documentReference : list) {
                            Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
                            tasks.add(documentSnapshotTask);
                        }
                        Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
                            @Override
                            public void onSuccess(List<Object> list) {
                                //Do what you need to do with your list
                                for (Object object : list) {
                                    TeacherPojo tp = ((DocumentSnapshot) object).toObject(TeacherPojo.class);
                                    Log.d("TAG", tp.getFirstName());
                                }
                            }
                        });
                    }
                }
            }
        });
    }
    

    So the List<Object> list is actually the list that contains objects of type TeacherPojo.

    0 讨论(0)
提交回复
热议问题