Know when addChildEventListener has finished

0

I have an android application in which I have a Fragment that contains a CardView. The CardView data is data of events that I have saved in Firebase. The problem I have is that when loading the Fragment there are times that it shows empty because it has not loaded all the data of Firebase and it takes a few seconds to appear the CardView with all the data. For this reason I have thought about introducing a ProgressDialog to show a waiting message until the loading of the data is finished and once it has loaded all this is removed.

I tried to put it into the method but I can not get it to work. I show you my method for loading Firebase data (data loading works perfectly).

private void cargarEventosDisponibles() {
        progressCargarEventos.setMessage("Cargando Eventos Disponibles.Por favor espere...");
        progressCargarEventos.show();
        listEventosDisponibles = new ArrayList<>();

        databaseEventos.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                Evento evento = dataSnapshot.getValue(Evento.class);
                listEventosDisponibles.add(evento);
                adaptador.setListaEventos(listEventosDisponibles);
                adaptador.notifyDataSetChanged();

            }

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

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                Evento evento = dataSnapshot.getValue(Evento.class);
                listEventosDisponibles.remove(evento);
                adaptador.setListaEventos(listEventosDisponibles);
                adaptador.notifyDataSetChanged();
            }

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

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        progressCargarEventos.dismiss();
    }

Thank you very much everyone.

    
asked by Josemanuu 19.12.2018 в 17:16
source

2 answers

1

The events that contain Value are executed after the child events, what you can do is generate with the same reference a listener for when your child events end, for example

//Aca sabemos que los eventos child terminaron
    databaseEventos.addValueEventListener(new ValueEventListener() {
        public void onDataChange(DataSnapshot dataSnapshot) {
          adaptador.notifyDataSetChanged();
          progressCargarEventos.dismiss();
        }
        public void onCancelled(FirebaseError firebaseError) { }
    });

    databaseEventos.addChildEventListener(new ChildEventListener() {

                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                    Evento evento = dataSnapshot.getValue(Evento.class);
                    listEventosDisponibles.add(evento);
                    adaptador.setListaEventos(listEventosDisponibles);


                }

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

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                    Evento evento = dataSnapshot.getValue(Evento.class);
                    listEventosDisponibles.remove(evento);
                    adaptador.setListaEventos(listEventosDisponibles);

                }

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

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


        }

That way, every time a child event makes an action, the addValueEventlistener event will be triggered by updating your adapter, that way you can be sure when it finishes loading the values in certain children.

    
answered by 21.12.2018 / 00:05
source
0

You can not know by child event listener how many children it will bring, and by executing progressCargarEventos.dismiss() outside of the call to Firebase and since the latter is asynchronous it will be executed immediately after progressCargarEventos.show() (so it will not be will show).

But it's a lot of data (100/1000 I've tried it and it worked for me) you can use (instead of ChildEventListener ) ValueEventListener , since it brings all the data together and one of the methods is the amount of Childs that he brought. Run with one for each children of the Firebase snapshot (it is perfectly explained in its documentation) and at the end of the for the dismiss of the progress dialog.

I hope it serves you. It is a less scalable solution than the one you propose but more feasible to interact with the user. Regards!

    
answered by 20.12.2018 в 21:42