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

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

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

package com.kaju.activities.workingActivity.Repository;

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import com.kaju.activities.workingActivity.Model.ProviderOfferModel;
import com.kaju.activities.workingActivity.Model.ProviderProfileModel;

import java.util.List;

public class FirestoreOfferRepository {
    //We get the currently linked firebase firestore database
    private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    private CollectionReference offersReference = firebaseFirestore.collection("offers");
    private OnFirestoreTaskComplete onFirestoreTaskComplete;

    public  FirestoreOfferRepository(OnFirestoreTaskComplete onFirestoreTaskComplete){
        this.onFirestoreTaskComplete = onFirestoreTaskComplete;
    }

    public void getOffersData(final String annoucementID){
        Query offersForAnnoucement = offersReference.whereEqualTo("announcementID", annoucementID);

        offersForAnnoucement.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    final List<ProviderOfferModel> newOffers = task.getResult().toObjects(ProviderOfferModel.class);

                    //We try to get the information for each user
                    for(final ProviderOfferModel currentOffer : newOffers){
                        String userID = currentOffer.getUserID();
                        getUserProfileData(new UserDataCallback() {
                            @Override
                            public void userReadDataCallback(ProviderProfileModel currentUser) {
                                int index = newOffers.indexOf(currentOffer);
                                newOffers.get(index).setProvider(currentUser);
                                 //This part here is executed only after the loop is over 
                            }
                        }, userID);

                    }
                     //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     // I want to wait here so the above callBacks are finished

                     //And we send the data to the outside layers
                    onFirestoreTaskComplete.offerDataAdded(newOffers);
                }else{
                    onFirestoreTaskComplete.onError(task.getException());
                }
            }
        });
    }  

    public void getUserProfileData(final UserDataCallback userDataCallback, String userID)
    {
        firebaseFirestore.collection("users").document(userID).get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if(task.isSuccessful()) {
                            ProviderProfileModel currentUser = task.getResult().toObject(ProviderProfileModel.class);
                            userDataCallback.userReadDataCallback(currentUser);
                        }

                    }
                });
    }

    public interface OnFirestoreTaskComplete{
        void offerDataAdded(List<ProviderOfferModel> offerRepositoryData);
        void onError(Exception e);
    }

    public interface UserDataCallback{
        void userReadDataCallback(ProviderProfileModel currentUser);
    }

}

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

wko9yo5t

wko9yo5t1#

所以我的做法是这样的:

public void getOffersData(final String annoucementID){
    Query offersForAnnoucement = offersReference.whereEqualTo("announcementID", annoucementID);

    offersForAnnoucement.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if(task.isSuccessful()){
                final List<ProviderOfferModel> newOffers = task.getResult().toObjects(ProviderOfferModel.class);

                //We try to get the information for each user
                for(final ProviderOfferModel currentOffer : newOffers){
                    String userID = currentOffer.getUserID();
                    getUserProfileData(new UserDataCallback() {
                        @Override
                        public void userReadDataCallback(ProviderProfileModel currentUser) {
                            int index = newOffers.indexOf(currentOffer);
                            newOffers.get(index).setProvider(currentUser);
                            onFirestoreTaskComplete.offerDataAdded(newOffers);
                        }
                    }, userID);

                }
                 //And we send the data to the outside layers
            }else{
                onFirestoreTaskComplete.onError(task.getException());
            }
        }
    });
}

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

相关问题