I have an Android Firebase with a section that contains a list of objects (numbered 1 to ~500). I am working on a code that selects six of these objects at random and returns them as a list. I have it working, but I am convinced that it is a very inefficient method. I currently generate a list of six random integers and use addListenerForSingleValueEvent six separate times to pull each object.
Is it possible to simplify this and build a HashMap so that I have one listener that pulls all six objects as a list?
I am currently using the method below. I call it six times (once for each random number), set a DatabaseReference based on the passed random integer, and add the return value to a list.
private void getRandomObject(int objectIdNumber){
DatabaseReference objectRef = baseRef.child(Constants.FB_LOCATION_OBJECTS)
.child(Integer.toString(objectIdNumber));
objectRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getValue());
//The list below is the global ArrayList<Object>
objectOptionsList.add(snapshot.getValue(POJOObject.class));
numberOfObjectsToGet--;
if (numberOfObjectsToGet == 0) {
proceed();
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}