I recently started switching my application from Parse to Firebase. Everything is fine so far, but I have not been able to find the equivalent Firebase method for Parse whereContainedIn (String key, Collection values).
It was a very useful method that allowed me to pass an array of identifiers, and it will return all the strings matching this id. So far with Firebase, I have been returning all rows in the database and then scrolling through them to check if this row id is in my identifier array. Another way is to request each identifier separately, which does not work with the asynchronous Firebase behavior. This is what I am doing now for the first approach:
List<String> userIds = new ArrayList<>();
userIds.add("2");
userIds.add("32");
userIds.add("12545");
userIds.add("187");
DatabaseReference firebaseRef = FirebaseDatabase.getInstance().getReference();
Query queryRef = firebaseRef.child("videos");
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Video> usersVideos = new ArrayList<Video>();
for (DataSnapshot videoSnapshot : dataSnapshot.getChildren()) {
Video video = videoSnapshot.getValue(Video.class);
if (userIds.contains(video.userId)) {
usersVideos.add(video);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "CANCELLED");
}
});
, .
!