У меня есть коллекция Posts
, в которой каждый документ содержит эти поля userId, postImage, uploadTime ...
Я хочу получить информацию о пользователе (userName, userImage
) из коллекции Users
, используя userId
поле Posts
collection и объединить его с PostModel
public class PostModel {
private String userId;
private String userName;
private String userImage;
private String postImage;
private long likeCount;
private long dislikeCount;
private Timestamp uploadTime;
public PostModel() {
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserImage() {
return userImage;
}
public void setUserImage(String userImage) {
this.userImage = userImage;
}
public String getPostImage() {
return postImage;
}
public void setPostImage(String postImage) {
this.postImage = postImage;
}
public long getLikeCount() {
return likeCount;
}
public void setLikeCount(long likeCount) {
this.likeCount = likeCount;
}
public long getDislikeCount() {
return dislikeCount;
}
public void setDislikeCount(long dislikeCount) {
this.dislikeCount = dislikeCount;
}
public Timestamp getUploadTime() {
return uploadTime;
}
public void setUploadTime(Timestamp uploadTime) {
this.uploadTime = uploadTime;
}
}
В настоящее время я так запрашиваю, но я не могу отслеживать , если все запросы на выборку Каждый Users
документ, связанный с Posts
завершен?
seepakDatabase.collection(COLLECTION_POSTS).orderBy(SORT, Query.Direction.DESCENDING).limit(LIMIT).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
// Clearing the previous data of the lists if any.
postModelList.clear();
if (queryDocumentSnapshots.getDocuments().size() < LIMIT) {
setNoMoreData(true);
} else {
setNoMoreData(false);
lastDocumentSnapshot = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
}
for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
final PostModel postModel = queryDocumentSnapshot.toObject(PostModel.class);
String userId = postModel.getUserId();
// How to know if all the queries made here completed?
seepakDatabase.collection(COLLECTION_USERS).document(userId).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
postModel.setUserName(documentSnapshot.getString("userName"));
postModel.setUserImage(documentSnapshot.getString("userImage"));
postModelList.add(postModel);
}
});
}
// Hiding the swipe refresh progress bar if visible.
postsSwipeRefresh.setRefreshing(false);
// Hiding progress bar when lists populate if visible.
postFetch.setVisibility(View.GONE);
// Notifying adapter to load new data in recycler view.
feedAdapter.notifyDataSetChanged();
}
});
Обновление 1
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
final PostModel postModel = queryDocumentSnapshot.toObject(PostModel.class);
Task<DocumentSnapshot> documentSnapshotTask = seepakDatabase.collection(COLLECTION_USERS).document(postModel.getUserId()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
postModel.setUserName(documentSnapshot.getString("userName"));
postModel.setUserImage(documentSnapshot.getString("userImage"));
postModelList.add(postModel);
}
});
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> objects) {
// Hiding the swipe refresh progress bar if visible.
postsSwipeRefresh.setRefreshing(false);
// Hiding progress bar when lists populate if visible.
postFetch.setVisibility(View.GONE);
// Notifying adapter to load new data in recycler view.
feedAdapter.notifyDataSetChanged();
}
});