0
votes

in a Firebase Android app that I'm currently developing I would like to provide an export feature. This feature should allow the user to export a set of data that is stored in Firebase.

My plan is to gather all required data into a intermediate object (datastructure) that can be (re-)used for multiple export types.

I am running into the issue that because of the flat Firebase data structure that I am using (as explained in https://www.firebase.com/docs/android/guide/structuring-data.html), it's difficult to know when all the data required for the export has been collected.

Example: when retrieving all objects that are referenced using 'indices' (name: key, value true), for each of these I set an addListenerForSingleValueEvent listener, but because this returns asynchronous, it's impossible to determine when all the indices are retrieved. This way it's not possible to determine the correct moment to start the export.

Who has best practices for coping with this?

1
One way is to keep a count of the indices that you started loading and decrement when one returns. When you reach 0, you're done. - Frank van Puffelen
Are you exporting this data to a flat ascii file? - Jay
@Jay no, not in this situation, but perhaps in a future app. Why are you asking? - Peter
@FrankvanPuffelen, I'm currently working on this approach. I will post a small code example once I have something working. - Peter

1 Answers

1
votes

Posting this to show a worked out example of the comment of @FrankvanPuffelen that seems to do the job quite well:

@Override
public void onDataChange(DataSnapshot indexListDataSnapshot) {
    final long participantsRequired = indexListDataSnapshot.getChildrenCount();

    for (DataSnapshot ds : indexListDataSnapshot.getChildren()) {
        DataUtil.getParticipantByKey( mEventKey, ds.getKey() ).addListenerForSingleValueEvent( new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Participant p = dataSnapshot.getValue(Participant.class);
                mParticipants.add( p );

                if (participantsRequired == mParticipants.size()){
                    executeExport();
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mListener.onDataLoadFailure( firebaseError.toException() );
            }
        });
    }
}