简体   繁体   English

Android Firestore将文档引用数组转换为List <Pojo>

[英]Android Firestore convert array of document references to List<Pojo>

In my Firestore I've got a users collection, within which the documents could have a bookmarks field, which is an array of references: 在我的Firestore中,我有一个users集合,其中的文档可以具有一个bookmarks字段,该字段是引用数组:

Firestore书签示例

Each of these references points to a document in the teachers collection: 这些参考文献均指向teachers集中的一个文档:

老师的例子

In my Android app, I want to create a method getBookmarks which returns a List of POJOs each of which represents a teacher. 在我的Android应用程序中,我想创建一个getBookmarks方法,该方法返回一个POJO List ,每个POJO都代表一个老师。 This is what I coded, but I think there are two problems: 这是我编写的代码,但是我认为有两个问题:

  1. I can't return a List<TeacherPojo> to my callback , because I'm getting each document reference singularly 我无法将List<TeacherPojo>返回到我的callback ,因为我会单个获取每个文档引用
  2. I think that attaching a callback for every item in a collection (which size it's user controlled, a user can have as many bookmarks as he/she wants) could have a high impact on performances. 我认为为集合中的每个项目附加一个回调(由用户控制大小,用户可以根据需要添加任意数量的书签)可能会对性能产生很大影响。

     public void getBookmarks(@NonNull OnSuccessListener<List<TeacherPojo>> callback) { checkNotNull(callback); // document reference points to the user document which is calling this method documentReference.get() .addOnSuccessListener((documentSnapshot) -> { ArrayList<DocumentReference> teacherReferences = (ArrayList<DocumentReference>) documentSnapshot.get("bookmarks"); Iterables.forEach(teacherReferences, (documentReference) -> { documentReference.get() .addOnSuccessListener((teacherSnapshot) -> { TeacherPojo teacherPojo = teacherSnapshot.toObject(TeacherPojo.class); // now? }); }); }); } 

Is there a better way to code this method, in order to get a List<TeacherPojo> (and possibly without having too impact on performances)? 为了获得List<TeacherPojo> (并且可能不会对性能产生太大影响),是否有更好的方法来编码此方法?

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 . 因此, List<Object> list实际上是包含TeacherPojo类型的对象的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM