Pergunta

Alguns principal código com o primeiro firebase chamada:

     refFB.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
                FirebaseReq fbReq = snapshot.getValue(FirebaseReq.class);
                service(fbReq);
            }
...
        });

Para manutenção e legibilidade, para mim, é muito mais claro este:

Run service(fbReq) in new thread.

 public void service(FirebaseReq firebaseReq) {
            value = dao(firebaseReq);
            /*some other code which use value*/
 }

public String dao(FirebaseReq firebaseReq) {
        String result = null;
        //the second firebase call
        childRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
               result = snapshot.getName();
            }
          ...
       });
        while (result== null){
        }   
        return result;
}

Ou é melhor evitar linhas e fila de espera, mas com unreadability código:

 public void service(FirebaseReq firebaseReq) {
     ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
               /*some other code which use value*/
            }
          ...
       });
     dao(firebaseReq,valueEventListener);              
 }

public String dao(FirebaseReq firebaseReq,ValueEventListener valueEventListener) {
        //the second firebase call
        childRef.addListenerForSingleValueEvent(valueEventListener);
}

Obrigado pela resposta

Foi útil?

Solução

Retornos de chamada assíncronos são quase sempre preferiu esperar.Especialmente uma ocupado esperar que você está usando.

Eu encontrar o seu código de retorno de chamada de limpeza na verdade.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top