[英]Dealing with Asynchronous Listeners in Firebase to Retrieve Data
I am learning to use Firebase and want to know if I am doing it right. 我正在学习使用Firebase,并想知道我是否做对了。 If I understood correctly, you can only retrieve data asynchronously using a listener in Firebase.
如果我理解正确,则只能使用Firebase中的侦听器异步检索数据。 I will try to explain my question with an example.
我将尝试通过一个例子来解释我的问题。 Say I have the following database data for a simple chat application:
说我有一个简单的聊天应用程序的以下数据库数据:
chat_info:
chatID_1:
participants:
uId_1: true
uId_2: true
users:
uId_1:
display_name: "David"
participated_chats:
chatID_1: true
chatID_2: true
uId_2:
display_name: "Jason"
participated_chats:
chatID_1: true
chatID_2: true
Now, I am trying to list the chats that David is participated in. So I do something like the following: 现在,我试图列出David参与的聊天。因此,我执行以下操作:
ArrayList<String> chatIdList = new ArrayList<String>();
// Retrieve chat Ids of participating chats
usersRef.child(mFirebaseUser.getUid()).child("participated_chats").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
chatIdList.clear();
// Save each chat Id to an arraylist
for (DataSnapshot child : dataSnapshot.getChildren()) {
chatIdList.add(child.getKey());
// when loop hits the last user of the dataSnapsot
if(chatIdList.size() >= dataSnapshot.getChildrenCount()) {
// For each chat Id, retrieve participants' uIds
for(String chatId : chatIdList) {
chatInfoRef.child(chatId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Chat chat = dataSnapshot.getValue(Chat.class); // In a Chat class, there is public Map<String, Boolean> participants = new HashMap<>();
chatDetailList.add(chat);
chatListAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Now, I have participants' uIds for each chat that a user is participated in. But, since I want to display the username, not the uId, I have to retrieve data from another node again. 现在,对于用户参与的每个聊天,我都有参与者的uId。但是,由于我想显示用户名而不是uId,因此我不得不再次从另一个节点检索数据。 Here is my worry because I have to add another asynchronous listner to retrieve data from different node.
这是我的担心,因为我必须添加另一个异步列表器才能从其他节点检索数据。 If it was something like MySQL, it would not be a problem, but Firebase listener is asynchronous.
如果是类似MySQL的东西,那不是问题,但是Firebase监听器是异步的。 This idea of asynchronous listener to retrieve data is very confusing and wonders if I am doing it right.
异步侦听器检索数据的想法非常令人困惑,想知道我是否做对了。 What should I do here?
我该怎么办?
You can just attach the first listener to the /users/uId_1
to get the whole user object, and then you can simply get the user's username / display name from the dataSnapshot value. 您只需将第一个侦听器附加到
/users/uId_1
即可获取整个用户对象,然后只需从dataSnapshot值中获取用户的用户名/显示名称即可。
Here's an example. 这是一个例子。
ArrayList<String> chatIdList = new ArrayList<String>();
// Retrieve chat Ids of participating chats
usersRef.child(mFirebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
chatIdList.clear();
User user = dataSnapshot.getValue(User.class);
String username = user.getDisplay_name();
Map<String, Boolean> participated_chats = user.getParticipated_chats();
// Save each chat Id to an arraylist
for (Map.Entry<String, Boolean> child : participated_chats.entries()) {
chatIdList.add(child.getKey());
// ... continues
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.