0

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();
Vincent Nagel
  • 63
  • 1
  • 9
  • Are you saying that you just want to query the database and get the raw data out of it? If that's all, don't use the Firebase UI library and instead query the database yourself using the Firestore SDK. – Doug Stevenson Sep 06 '19 at 22:54
  • I want to populate the recyclerview with the data from the database. I wanted to use the FirestoreRecyclerAdapter because it handles when items are added, modified, and removed. – Vincent Nagel Sep 06 '19 at 22:56
  • It sounds like you might not have gone through the entire documentation for Firebase UI. It describes how to bind data from snapshots to the views in your recyclerview. – Doug Stevenson Sep 06 '19 at 23:12
  • I understand that you need to specify a query that will be used to populate the recyclerview. The problem is that I can't come up with a query that will retrieve the data pointed to by the documentreference objects. I was trying to get around this by using a query that retrieves the documentreference objects and retrieving the data pointed to by those in parseSnapshot(). However, retrieving data is asynchronous so I don't know how to return it in parseSnapshot(). – Vincent Nagel Sep 07 '19 at 00:32

1 Answers1

0

You won't be able to do this because parseSnapshot needs to happen synchronously, but getting another document is asynchronous. The document get won't complete with a callback until after parseSnapshot returns. So, you won't be able to use Firebase UI for this - you'll have to come up with your own way of making multiple requests to populate the RecyclerView, and it will likely require a lot of work.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441