André Luiz
André Luiz

Reputation: 7332

Android + Firebase: synchronous for into an asynchronous function

I'm making a function (Java - Android) which should return me a list of objects filled with data from Firebase. My problem is that I need to send the result to the listener just after I loop through all the items in a for, take a look at the code:

final DatabaseReference beaconMessageRef = dataSnapshot.getRef().getRoot().child("region_messages/" + regionKey);
beaconMessageRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if(!dataSnapshot.exists())
            FirebaseHelper.this.messageByBeaconListener.onSuccess(null);

        for (DataSnapshot regionMessageDS : dataSnapshot.getChildren()) {

            RegionMessage regionMessage = regionMessageDS.getValue(RegionMessage.class);
            String msgKey = regionMessageDS.getKey();

            final DatabaseReference messageRef = regionMessageDS.getRef().getRoot().child("messages/"+msgKey);
            messageRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if(dataSnapshot.exists()){
                        Message msg = dataSnapshot.getValue(Message.class);
                        FirebaseHelper.this.addMessageByBeacon(msg);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    FirebaseHelper.this.messageByBeaconListener.onFail(databaseError);
                }
            });
        }

        List<Message> msgs = FirebaseHelper.this.beaconMessageList;
        FirebaseHelper.this.beaconMessageList = null;
        FirebaseHelper.this.messageByBeaconListener.onSuccess(msgs);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        FirebaseHelper.this.messageByBeaconListener.onFail(databaseError);
    }
});

I need to execute this line: FirebaseHelper.this.messageByBeaconListener.onSuccess(msgs); just after I loop through all the items in the for.

How can I achieve that?

Upvotes: 2

Views: 2156

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

Something that is asynchronous cannot reliably be made synchronous on Android. Whenever you feel the urge to do that, take a deep breath and repeat that first sentence. Or read my answer here: Setting Singleton property value in Firebase Listener

Instead you will have to do what everyone does: move the code that needs the data into the callback that is triggered when the data is available.

In your case it is a bit trickier, since you're loading multiple items. Luckily though you know how many items you need to load. So if you introduce a counter to track how many items have loaded, you will know when you're done:

final DatabaseReference beaconMessageRef = dataSnapshot.getRef().getRoot().child("region_messages/" + regionKey);
beaconMessageRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    if(!dataSnapshot.exists())
      FirebaseHelper.this.messageByBeaconListener.onSuccess(null);

    for (DataSnapshot regionMessageDS : dataSnapshot.getChildren()) {

      RegionMessage regionMessage = regionMessageDS.getValue(RegionMessage.class);
      String msgKey = regionMessageDS.getKey();

      final Integer[] counter = new Integer[1];
      final DatabaseReference messageRef = regionMessageDS.getRef().getRoot().child("messages/"+msgKey);
      messageRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot innerSnapshot) {
          if(innerSnapshot.exists()){
            Message msg = innerSnapshot.getValue(Message.class);
            FirebaseHelper.this.addMessageByBeacon(msg);
          }
          counter[0] = new Integer(counter[0].intValue()+1);
          if (counter[0].equalTo(dataSnapshot.numChildren()) {
            List<Message> msgs = FirebaseHelper.this.beaconMessageList;
            FirebaseHelper.this.beaconMessageList = null;
            FirebaseHelper.this.messageByBeaconListener.onSuccess(msgs);
          }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
          FirebaseHelper.this.messageByBeaconListener.onFail(databaseError);
        }
      });
    }
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    FirebaseHelper.this.messageByBeaconListener.onFail(databaseError);
  }
});

See this answer for an explanation of the single-element-array: https://stackoverflow.com/a/5977866/209103

Upvotes: 1

Related Questions