本文介绍了正在等待回报,好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
第一个firebase调用的一些主要代码:$ b $ pre $ ref $ B $ refFB.addChildEventListener(new ChildEventListener(){
@覆盖
public void onChildAdded(DataSnapshot snapshot,String previousChildName){
FirebaseReq fbReq = snapshot.getValue(FirebaseReq.class);
service(fbReq);
}
...
});
为了保养和可读性,对我来说更加清楚了:
在新线程中运行服务(fbReq)。
public void service(firebaseReq firebaseReq){
value = dao(firebaseReq);
/ *一些使用值的其他代码* /
}
public String dao(FirebaseReq firebaseReq){
String result = null;
//第二个firebase调用
childRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
result = snapshot.getName ();
}
...
});
while(result == null){
}
返回结果;或者更好地避免线程和等待循环,但是不可读代码:
p>
public void service(FirebaseReq firebaseReq){
ValueEventListener valueEventListener = new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
/ *其他一些使用值的代码* /
}
...
});
dao(firebaseReq,valueEventListener);
public String dao(firebaseReq firebaseReq,ValueEventListener valueEventListener){
//第二个firebase调用
childRef.addListenerForSingleValueEvent(valueEventListener);
感谢您的回复
异步回调几乎总是首选等待。特别是你正在使用的一个繁忙的等待。
我发现你的回调代码实际上是清洁的。
Some main code with the first firebase call:
refFB.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
FirebaseReq fbReq = snapshot.getValue(FirebaseReq.class);
service(fbReq);
}
...
});
For maintanance and readability is for me much more clear this:
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;
}
Or is better avoid threads and waiting loop, but with unreadability code:
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);
}
Thanks for reply
解决方案 Async callbacks are almost always preferred to a wait. Especially a busy wait as you are using.
I find your callback code cleaner in fact.
这篇关于正在等待回报,好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-06 00:00