My data is set up as follows:
- The Users collection contains User documents.
- A User document contains a Friends subcollection.
- A Friends subcollection contains UserRef documents.
- A UserRef document contains a DocumentReference to a User document.
I want to display all the friends of a particular user in a RecyclerView using the FirestoreRecyclerAdapter. From this answer, it seems like I cannot retrieve the User documents pointed to by a DocumentReference in a query. So, the following code is my attempt at using a SnapshotParser to do so. However, I don't know how to can return the User from parseSnapshot() since it is retrieved asynchronously.
Query query = friendsSubcollection;
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class, new SnapshotParser<User>() {
@NonNull
@Override
public User parseSnapshot(@NonNull DocumentSnapshot snapshot) {
DocumentReference userRef = snapshot.toObject(DocumentReference.class);
userRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
User user = documentSnapshot.toObject(User.class); // How do I return this user from parseSnapshot()?
}
});
}
})
.build();