[英]How to get list of Firestore users by its uid?
I have a method for get a List of users by its uid
: 我有一种通过
uid
获取用户列表的方法:
public void getNeededUsers(List<String> uidList, UsersListCallback usersListCallback) {
CollectionReference users = db.collection(Consts.USERS_DB);
for (String uid: uidList) {
users.whereEqualTo(Consts.UID, uid).get()
.addOnCompleteListener(task -> {
List<FsUser> fsUserList = new ArrayList<>();
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
fsUserList.add(snapshot.toObject(FsUser.class));
}
usersListCallback.getUsers(fsUserList);
});
}
}
But it seems that this method doesn`t work properly. 但似乎这种方法无法正常工作。 When I try to iterate through the List of users in another method, I receive users one by one from callback, instead of getting all, so they are duplicated in my list.
当我尝试用另一种方法遍历用户列表时,我从回调中逐个接收用户,而不是全部接收,因此它们在我的列表中重复。
Here is a code of another method: 这是另一种方法的代码:
public void createUserChats(UserChatListCallback userChatListCallback) {
final List<UserChat> userChatList = new ArrayList<>();
getChatAndUidList((chatList, uidList) -> {
getRtUsersAndMessages(uidList, (rtUserList, messageList) -> {
getNeededUsers(uidList, userList -> {
if (!userList.isEmpty()) {
for (int i = 0; i < userList.size(); i++) {
String name = userList.get(i).getName();
String image = userList.get(i).getImage();
String userId = userList.get(i).getUid();
long timeStamp = chatList.get(i).getTimeStamp();
boolean seen = chatList.get(i).getSeen();
String online = rtUserList.get(i).getOnline();
String message = messageList.get(i).getMessage();
long messageTimestamp = messageList.get(i).getTime();
userChatList.add(new UserChat(name, image, timeStamp, seen, userId, online,
message, messageTimestamp));
}
}
userChatListCallback.getUserChatList(userChatList);
});
});
});
}
To get a list of users, please use the following code: 要获取用户列表,请使用以下代码:
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String uid: uidList) {
Task<DocumentSnapshot> documentSnapshotTask = users.whereEqualTo(Consts.UID, uid).get();
tasks.add(documentSnapshotTas);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your users list
for (Object object : list) {
FsUser fsUser = ((DocumentSnapshot) object).toObject(TeacherPojo.class);
Log.d("TAG", fsUser.getName());
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.