Sta aspettando il ritorno, ok?
Domanda
Qualche codice principale con la prima chiamata di FireBase:
refFB.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
FirebaseReq fbReq = snapshot.getValue(FirebaseReq.class);
service(fbReq);
}
...
});
.
Per la manutenzione e la leggibilità è per me molto più chiaro questo:
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;
}
.
o è meglio evitare fili e loop di attesa, ma con il codice illepedevole:
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);
}
.
Grazie per la risposta
Soluzione
I callback ASYNC sono quasi sempre preferiti a un'attesa.Soprattutto un'attesa occupata come stai usando.
Trovo il tuo cleaning del codice di richiamata in effetti.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow