1
與第一火力點調用一些主要代碼:正在等待退貨,好嗎?
refFB.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
FirebaseReq fbReq = snapshot.getValue(FirebaseReq.class);
service(fbReq);
}
...
});
對於maintanance和可讀性是對我更清楚這一點:
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;
}
或者是更好的避免線程和等待循環,但不可讀性代碼:
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);
}
感謝您的回覆
while while while(result == null){}'可能會消耗大量的處理器時間。如果您決定這樣做,請考慮使用[wait](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait())和[notify ](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notify())而不是循環。 – ajb