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:
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:
- I can't return a
List<TeacherPojo>
to mycallback
, because I'm getting each document reference singularly 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)?