Android Firestore将文档引用数组转换为List< Pojo> [英] Android Firestore convert array of document references to List<Pojo>

查看:120
本文介绍了Android Firestore将文档引用数组转换为List< Pojo>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firestore中,我有一个 users 集合,其中的文档可以有一个 bookmarks 字段,这是一系列参考文献:

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

每个这些引用指向教师集合中的文档:

Each of these references points to a document in the teachers collection:

在我的Android应用程序中,我想创建一个方法 getBookmarks ,它返回 List POJO,每个都代表一名教师。这是我编码的,但我认为有两个问题:

In my Android app, I want to create a method getBookmarks which returns a List of POJOs each of which represents a teacher. This is what I coded, but I think there are two problems:


  1. 我无法返回列表< TeacherPojo> 到我的回调,因为我单独得到每个文档的引用

  2. I认为为集合中的每个项目附加一个回调(用户控制的大小,用户可以拥有他/她想要的书签数量)可能会对性能产生很大的影响。

  1. I can't return a List<TeacherPojo> to my callback, because I'm getting each document reference singularly
  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?
                });
            });
        });
}


有没有更好的编码此方法的方法,以获得列表< TeacherPojo> (并且可能对性能没有太大影响)?

Is there a better way to code this method, in order to get a List<TeacherPojo> (and possibly without having too impact on performances)?

推荐答案

是的。请参阅以下代码行:

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

所以列表< Object> list 实际上是包含 TeacherPojo 类型对象的列表。

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

这篇关于Android Firestore将文档引用数组转换为List&lt; Pojo&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆