我正在开发一个聊天应用程序,但是在查看用户的好友请求时,我遇到的问题是
当我记录Firebase的查询时,为什么不能正确地将其作为代码订购?
这是我的代码:
// Query for pending requests in the current user
Log.v("AAAAAA", "AAAAAA");
usersRef.child(MainActivity.currentUser.getUid()).child("inpendingfriendreq").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) { // dataSnapShot = inpendingfriendreq node
for (DataSnapshot reqDataSnapShot : dataSnapshot.getChildren()) {
U = reqDataSnapShot;
Log.v("BBBBBBBB", U.toString());
usersRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot sDataSnapshot) { // sDataSnapshot = "users" node DataSnapShot
for (DataSnapshot searchForPendingUserDS : sDataSnapshot.getChildren()) { // searchForPendingUserDS = a user from database (child node of "users" node)
Log.v("CCCCCCC", searchForPendingUserDS.toString());
if (searchForPendingUserDS.getKey().equals(U.getKey())) { // If that user's id equals pending req id
MainActivity.reqArrList.add(new FriendRequest(R.drawable.default_pp, searchForPendingUserDS.child("name").getValue(String.class)));
Log.v("DDDDDDDDD", searchForPendingUserDS.child("name").getValue(String.class));
}
}
Log.v("EEEEEEEE", MainActivity.reqArrList.toString());
//requestsPageUpdated();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(mContext, "Error querying", Toast.LENGTH_SHORT).show();
}
});
//requestsPageUpdated();
Log.v("FFFFFFFFFF", "FFFFFFFFF");
break;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(mContext, "Error querying", Toast.LENGTH_SHORT).show();
}
});
应该是:
AAAAAA
BBBBBB
CCCCCC
DDDDDD
CCCCCC
CCCCCC
CCCCCC
EEEEEE
FFFFFF
GGGGGG
但是它在Logcat中表明了这一点:
AAAAAA
GGGGGG
BBBBBB
FFFFFF
CCCCCC
DDDDDD
CCCCCC
CCCCCC
CCCCCC
EEEEEE
我知道事件监听器是异步的,但是这是什么意思呢? 异步是意味着该函数正在等待像按钮onClickListener一样被触发,还是意味着它在被触发时与主线程并行运行?所以在第二种情况下,这对我的问题有意义。 请帮忙!