2

Currently I have a problem with a Firebase query that is not executing the onChildAdded method. I implemented a callback to handle the asynchronous nature of Firebase queries, therefore, after having populated thesectoresMayorEspera and localizacionMayorEspera ArrayLists inside the onChildAdded method using data contained in the dataSnapshot, the buscarEspera method should be executed with the previous ArrayLists as parameters.

The problem arises when onChildAdded method is "ignored" and the buscarEspera method is passed empty ArrayLists.

    public void busquedaEspera(final MiCallbackEspera miCallbackEspera) {

    if (cantidadBuses > 1) {
        Firebase sectoresMasTiempo = new Firebase("https://brilliant-heat-7882.firebaseio.com/SectoresDatos/").child(String.valueOf(sectorOrigenBus));
        sectoresMasTiempo.orderByChild("espera").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                sectoresMayorEspera.add(dataSnapshot.getKey());
                HashMap hashMap = (HashMap) dataSnapshot.getValue();
                HashMap localizacionHashMap = (HashMap) hashMap.get("localizacionEspera");
                localizacionMayorEspera.add(new LatLng((double) localizacionHashMap.get("latitude"), (double) localizacionHashMap.get("longitude")));
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        miCallbackEspera.buscarEspera(sectoresMayorEspera, localizacionMayorEspera);
    }

I am new to coding (obviously new to Android, to Java, to...) I'd appreciate your help to tackle this one.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The `onChildAdded` method will be triggered after your call to `miCallbackEspera.buscarEspera`. See this question where I wrote up how to either work around this or, preferably, how to properly use it. http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener/33204705#33204705 – Frank van Puffelen Nov 16 '15 at 17:31
  • 1
    Thank you very much Frank, the question you pointed out was extremely helpful, placing the callback method inside the onChildAdded method besides some other refactoring to properly work with the ArrayLists as they update saved me from drowning, you truly have to "embrace" the asynchronous character of Firebase if you pretend to succeed working with it. – Hamilton Zambrano Nov 17 '15 at 00:09
  • You're taking the red pill? Awesome! – Frank van Puffelen Nov 17 '15 at 01:16

0 Answers0