Java/Firestore – 在循环至下一项之前等待数据被读取(异步)

huangapple go评论83阅读模式
英文:

Java/Firestore - Wait for data to be read before looping to next item (Async)

问题

我正在尝试在我的项目中实现一个VMMV架构,以便在对象之间实现低耦合。

我的Firestore数据库中有两个实体:Offers(优惠)和Users(用户)。

每个优惠都是由一个用户发布的。

在检索到所有优惠后,我想将有关用户的信息(姓名、个人头像、描述)添加到OffersViewModel(以便将其绑定到我拥有的RecicleView)。

问题是,在将优惠传递给外部层之前,我无法同步读取用户配置文件信息(以便可以将它们绑定在一个模型中)。

以下是我拥有的内容:

// 代码部分已略去

我在YT上找到了一个教程,解释了我应该如何从回调中检索数据,但似乎不起作用(或者我实现得不好)。

我已经尝试过一些调试方法,比如使用日志记录,对回调进行封装,执行顺序如下:

1- 循环之前 -> 列表未更改
2- 循环内 -> 列表未更改
3- 循环之后 -> 列表未更改
4- 在回调内 -> 列表已更改

对于这个问题,我非常感谢任何帮助!

英文:

I am trying to implement a VMMV architecture on my project in order to have low coupling between objects.

I have 2 entities in my Firestore database: Offers and Users.

Each offer is posted by a user.

After retrieving all offers, I want to add information about the user (name, profileImage, description) to the OffersViewModel (to bind it to the RecicleView I have).

The problem is I cannot sync the reading of user profile information before the offers are being passed to the outer layers(so I can bind them in 1 model)

Here is what I have:

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);
    }

   



}

I found a tutorial on YT which explains how I should retrieve the data from the callback but it doesn't seem to work(or I implemented it badly).

I have tried some debugging with Logs, surrounding the callback, and the order of executing is like this:

1-Before loop -> List unchanged

2-In loop -> List unchanged

3-After loop -> List unchanged

4-In callBack -> List changed

I appreciate any help on the matter !

答案1

得分: 1

以下是翻译好的内容:

这样,我最终做的方式是这样的:

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);

                // 我们尝试获取每个用户的信息
                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);

                }
                 // 然后我们将数据发送给外部层
            }else{
                onFirestoreTaskComplete.onError(task.getException());
            }
        }
    });
}

基本上,每当我为1个以上的用户检索数据时,我都会将包含用户信息的新更新列表发送到ViewModel和LiveData(发送到外部层)。

它能工作,我认为这是因为LiveData的监听器onChanged会在新信息写入时更新适配器。

即使它能工作,我认为这还有点不够完善。我会欣赏其他获得这个(预期)结果的方法。

英文:

So the way I did it after all is like this:

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

    offersForAnnoucement.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
            if(task.isSuccessful()){
                final List&lt;ProviderOfferModel&gt; 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());
            }
        }
    });
}

Basically, every time I retrieve data for 1 more user, I send the newly updated list with the user information to the ViewModel and to LiveData (to the outside layer).

It works and I think it does because of the LiveData listener onChanged which updates the Adapter everytime new information is written.

Even if it works, I think is a bit sketchy. I would appreciate any other way to obtain this (expected) result

huangapple
  • 本文由 发表于 2020年8月21日 01:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定