Skip to main content
Solucion parche al problema
Origen Enlace

============== Edito ============

Le agregue un contador para saber en que momento hacerle el callback.

public synchronized void next(String key,final RoomListQueryResultHandler handler) {
   
        this.setLoading(true);
        roomList = new ArrayList<Room>();
        this.database.child("members").child(this.mUser.getUid()).child("rooms")
                .orderByKey()
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        RoomListQuery.this.setLoading(false);
                        if (!dataSnapshot.hasChildren()) {
                            RoomListQuery.this.currentPage--;
                        }

                        // at the start we need to still load all children
                        final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };

                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            //roomList.add(Room.upsert(room));
                            Room.getRoom(ds.getKey(), new Room.RoomGetHandler() {
                                @Override
                                public void onResult(Room room, customException e) {
                                    if (e != null) {
                                        // Error!
                                        e.printStackTrace();
                                        return;
                                    }
                                    roomList.add(room);

                                    // we loaded a child, check if we're done
                                    pendingLoadCount[0] = pendingLoadCount[0] - 1;
                                    if (pendingLoadCount[0] == 0) {
                                        if (handler != null) {
                                            Friends.runOnUIThread(new Runnable() {
                                                public void run() {
                                                    handler.onResult(roomList, (customException) null);
                                                }
                                            });
                                        }
                                    }


                                }
                            });

                        }

                    }

                    @Override
                    public void onCancelled(final DatabaseError databaseError) {
                        if (handler != null) {
                            Friends.runOnUIThread(new Runnable() {
                                public void run() {
                                    handler.onResult((List) null, new customException(databaseError.toString()));
                                }
                            });
                        }
                    }
                });
    }

============== Edito ============

Le agregue un contador para saber en que momento hacerle el callback.

public synchronized void next(String key,final RoomListQueryResultHandler handler) {
   
        this.setLoading(true);
        roomList = new ArrayList<Room>();
        this.database.child("members").child(this.mUser.getUid()).child("rooms")
                .orderByKey()
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        RoomListQuery.this.setLoading(false);
                        if (!dataSnapshot.hasChildren()) {
                            RoomListQuery.this.currentPage--;
                        }

                        // at the start we need to still load all children
                        final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };

                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            //roomList.add(Room.upsert(room));
                            Room.getRoom(ds.getKey(), new Room.RoomGetHandler() {
                                @Override
                                public void onResult(Room room, customException e) {
                                    if (e != null) {
                                        // Error!
                                        e.printStackTrace();
                                        return;
                                    }
                                    roomList.add(room);

                                    // we loaded a child, check if we're done
                                    pendingLoadCount[0] = pendingLoadCount[0] - 1;
                                    if (pendingLoadCount[0] == 0) {
                                        if (handler != null) {
                                            Friends.runOnUIThread(new Runnable() {
                                                public void run() {
                                                    handler.onResult(roomList, (customException) null);
                                                }
                                            });
                                        }
                                    }


                                }
                            });

                        }

                    }

                    @Override
                    public void onCancelled(final DatabaseError databaseError) {
                        if (handler != null) {
                            Friends.runOnUIThread(new Runnable() {
                                public void run() {
                                    handler.onResult((List) null, new customException(databaseError.toString()));
                                }
                            });
                        }
                    }
                });
    }
Origen Enlace

Firebase / Android - Esperar todos los thread para un Handle

tengo el siguiente codigo:

public synchronized void next(final RoomListQueryResultHandler handler) {
        this.setLoading(true);

        roomList = new ArrayList<Room>();
        this.database.child("members").child(this.mUser.getUid()).child("rooms")
                .limitToFirst(this.mLimit)
                .startAt(this.currentPage * this.mLimit)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        RoomListQuery.this.setLoading(false);
                        //mListAdapter.setLoading(false);

                        if (!dataSnapshot.hasChildren()) {
                            RoomListQuery.this.currentPage--;
                        }
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            Room room = ds.getValue(Room.class);
                            //roomList.add(Room.upsert(room));
                            Room.getRoom(room.getId(), new Room.RoomGetHandler() {
                                @Override
                                public void onResult(Room room, customException e) {
                                    if (e != null) {
                                        // Error!
                                        e.printStackTrace();
                                        return;
                                    }

                                    roomList.add(room);
                                }
                            });
                            handler.onResult(roomList, (customException) null);
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        handler.onResult((List) null, new customException(databaseError.toString()));
                    }
                });
    }
}

Si se fijan tengo dos Handler, al principio llamo a una lista de "rooms" desde Firebase, y luego por cada una voy a consultar el detalle.

El problema que tengo es que la respuesta me la da vacia, ya que no espera que se ejecuten todos los for para el detalle de las "rooms", por lo cual la variable roomList siempre vuelve vacia.

Alguna idea de que puedo implementar, o que otra metodologia utilizar para poder resolverlo?

Muchas gracias! Saludos.