java/firestore-在循环到下一项之前等待读取数据(异步)

disho6za  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(417)

我试图在我的项目上实现一个vmmv架构,以便在对象之间具有低耦合。
我的firestore数据库中有两个实体:offers和users。
每个报价都由一个用户发布。
检索完所有的offers之后,我想向offersviewmodel添加关于用户的信息(name、profileimage、description)(将其绑定到我拥有的recicleview)。
问题是我不能同步读取用户配置文件信息之前,提供正在被传递到外层(所以我可以绑定他们在1模型)
以下是我所拥有的:

  1. package com.kaju.activities.workingActivity.Repository;
  2. import android.util.Log;
  3. import androidx.annotation.NonNull;
  4. import com.google.android.gms.tasks.OnCompleteListener;
  5. import com.google.android.gms.tasks.Task;
  6. import com.google.firebase.firestore.CollectionReference;
  7. import com.google.firebase.firestore.DocumentReference;
  8. import com.google.firebase.firestore.DocumentSnapshot;
  9. import com.google.firebase.firestore.FirebaseFirestore;
  10. import com.google.firebase.firestore.Query;
  11. import com.google.firebase.firestore.QuerySnapshot;
  12. import com.kaju.activities.workingActivity.Model.ProviderOfferModel;
  13. import com.kaju.activities.workingActivity.Model.ProviderProfileModel;
  14. import java.util.List;
  15. public class FirestoreOfferRepository {
  16. //We get the currently linked firebase firestore database
  17. private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
  18. private CollectionReference offersReference = firebaseFirestore.collection("offers");
  19. private OnFirestoreTaskComplete onFirestoreTaskComplete;
  20. public FirestoreOfferRepository(OnFirestoreTaskComplete onFirestoreTaskComplete){
  21. this.onFirestoreTaskComplete = onFirestoreTaskComplete;
  22. }
  23. public void getOffersData(final String annoucementID){
  24. Query offersForAnnoucement = offersReference.whereEqualTo("announcementID", annoucementID);
  25. offersForAnnoucement.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  26. @Override
  27. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  28. if(task.isSuccessful()){
  29. final List<ProviderOfferModel> newOffers = task.getResult().toObjects(ProviderOfferModel.class);
  30. //We try to get the information for each user
  31. for(final ProviderOfferModel currentOffer : newOffers){
  32. String userID = currentOffer.getUserID();
  33. getUserProfileData(new UserDataCallback() {
  34. @Override
  35. public void userReadDataCallback(ProviderProfileModel currentUser) {
  36. int index = newOffers.indexOf(currentOffer);
  37. newOffers.get(index).setProvider(currentUser);
  38. //This part here is executed only after the loop is over
  39. }
  40. }, userID);
  41. }
  42. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  43. // I want to wait here so the above callBacks are finished
  44. //And we send the data to the outside layers
  45. onFirestoreTaskComplete.offerDataAdded(newOffers);
  46. }else{
  47. onFirestoreTaskComplete.onError(task.getException());
  48. }
  49. }
  50. });
  51. }
  52. public void getUserProfileData(final UserDataCallback userDataCallback, String userID)
  53. {
  54. firebaseFirestore.collection("users").document(userID).get()
  55. .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
  56. @Override
  57. public void onComplete(@NonNull Task<DocumentSnapshot> task) {
  58. if(task.isSuccessful()) {
  59. ProviderProfileModel currentUser = task.getResult().toObject(ProviderProfileModel.class);
  60. userDataCallback.userReadDataCallback(currentUser);
  61. }
  62. }
  63. });
  64. }
  65. public interface OnFirestoreTaskComplete{
  66. void offerDataAdded(List<ProviderOfferModel> offerRepositoryData);
  67. void onError(Exception e);
  68. }
  69. public interface UserDataCallback{
  70. void userReadDataCallback(ProviderProfileModel currentUser);
  71. }
  72. }

我找到了一个关于yt的教程,它解释了我应该如何从回调中检索数据,但它似乎不起作用(或者我实现得很糟糕)。
我尝试了一些日志调试,围绕回调,执行顺序如下:
1-before循环->列表未更改
二进位循环->列表不变
三后循环->列表不变
4-in回调->列表已更改
我很感激你在这件事上的任何帮助!

wko9yo5t

wko9yo5t1#

所以我的做法是这样的:

  1. public void getOffersData(final String annoucementID){
  2. Query offersForAnnoucement = offersReference.whereEqualTo("announcementID", annoucementID);
  3. offersForAnnoucement.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  4. @Override
  5. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  6. if(task.isSuccessful()){
  7. final List<ProviderOfferModel> newOffers = task.getResult().toObjects(ProviderOfferModel.class);
  8. //We try to get the information for each user
  9. for(final ProviderOfferModel currentOffer : newOffers){
  10. String userID = currentOffer.getUserID();
  11. getUserProfileData(new UserDataCallback() {
  12. @Override
  13. public void userReadDataCallback(ProviderProfileModel currentUser) {
  14. int index = newOffers.indexOf(currentOffer);
  15. newOffers.get(index).setProvider(currentUser);
  16. onFirestoreTaskComplete.offerDataAdded(newOffers);
  17. }
  18. }, userID);
  19. }
  20. //And we send the data to the outside layers
  21. }else{
  22. onFirestoreTaskComplete.onError(task.getException());
  23. }
  24. }
  25. });
  26. }

基本上,每次我为另外一个用户检索数据时,我都会将包含用户信息的最新更新列表发送到viewmodel和livedata(发送到外层)。
它可以工作,我认为这是因为livedata监听器onchanged每次写入新信息时都会更新适配器。
即使成功了,我觉得还是有点粗略。我将很感激任何其他方式来获得这个(预期的)结果

展开查看全部

相关问题