Reputation: 13
I'm currently stuck trying to display a user's pending friend requests in a RecyclerView, using FirestoreRecyclerAdapter.
This is how my data is structured:
I would like to query something like this, getting all the user documents for which the userId is found in the array.
CollectionReference usersRef = db.collection("users");
Query query = usersRef.whereIn("userId", requestsArray);
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
And this is how I get the array of user id's from the database
currentUser = firebaseAuth.getInstance().getCurrentUser();
DocumentReference requestsRef = db.collection("friend requests").document(currentUser.getUid());
requestsRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot documentSnapshot = task.getResult();
Requests request = documentSnapshot.toObject(Requests.class);
requestsArray = request.getReceivedRequests();
}
}
});
What I'm struggling with is figuring out how to make sure that requests.get() has finished and the array is not null before trying to query the Users collection and populating the RecyclerView, because I'm getting the following error "java.lang.IllegalArgumentException: Invalid Query. A non-empty array is required for 'in' filters.".
I have to mention that I'm pretty new to Firebase and still learning how it all works. Any suggestions/help is very much appreciated!
Upvotes: 1
Views: 300
Reputation: 138979
What I'm struggling with is figuring out how to make sure that requests.get() has finished
You'll always be 100% sure that "requestsRef.get()" has finished loading the data from the database when the "onComplete()" method fires. This means that all the data at "requestsRef" location could be read, no security rule rejected the read operation and now all the data is available to be used in any other operation. So you always need to wait for the data to be available in order to be used in another query, for example.
and the array is not null before trying to query the Users collection and populating the RecyclerView
To solve this, you might consider instantiating both arrays, "receivedRequests" and "sentRequests" that exist in your "Requests" class. In this way, when no ID is present in an array, you'll get an empty array and not "null". If you don't want to do that, you can also check against nullity.
However, the following error:
java.lang.IllegalArgumentException: Invalid Query. A non-empty array is required for 'in' filters.
Occurs because by the time you are sending the "requestsArray" object to the "whereIn()" method, the data isn't finished loading yet from the database. So any code that needs data from the Firestore needs to be inside the "onComplete()" method, or be called from there. So the simplest solution in this case would be to move the following lines of code:
CollectionReference usersRef = db.collection("users");
Query query = usersRef.whereIn("userId", requestsArray);
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build()
Right inside the "onComplete()" method, after the following line of code:
requestsArray = request.getReceivedRequests();
In this way, you'll be sure that you'll never sure a null array. In the worst-case scenario, you'll use an empty array but not an unutilized one.
Upvotes: 1