3

I have a list of object keys that correspond to objects in my FB database. I want to retrieve all of those objects in an efficient way but I couldn't figure out how to query them.

For now I am using this code. It's just a recursive method that retrieves the objects one by one until they are all found. It can be very slow.

private void getNextPoll(){
        pollRef = rootRef.child(getString(R.string.poll_ref)).child(myPollIds.get(polls.size()));
        pollRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Poll poll = dataSnapshot.getValue(Poll.class);

                polls.add(poll);

                System.out.println(poll);

                //if this is the last poll, then sort the polls by date and display them
                if(polls.size() == myPollIds.size()){
                    //Done

                    //sort

                    //display
                    recyclerView.setAdapter(new MyPollsListAdapter(getContext(), polls));
                }else{
                    getNextPoll();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, databaseError.getMessage());
            }
        });
    }

My database is organized like so:

How should I go about doing this?

Thanks in advance :)

Jared
  • 2,029
  • 5
  • 20
  • 39
  • can you post sample of the db structure. – John O'Reilly Dec 17 '16 at 20:44
  • I have added a picture of what my database looks like – Jared Dec 17 '16 at 22:21
  • why not just create all of the eventListener and then attach them all? you can set the adapter without the polls and then just call `adapter.notifyDataSetChanged()` for every listener after the new poll data is added to the list. Or do you really want to make sure all the poll objects are retrieved before showing them to the user? – Wilik Dec 18 '16 at 16:22

0 Answers0