异步收集来自Firebase的数据:数据集何时完成? [英] Gathering data from Firebase asynchronously: when is the data-set complete?

查看:115
本文介绍了异步收集来自Firebase的数据:数据集何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的Firebase Android应用程序中,我想提供导出功能。此功能应允许用户导出存储在Firebase中的一组数据。



我的计划是将所有需要的数据收集到一个中间对象(数据结构) (重新)用于多种导出类型。

我遇到了这个问题,因为我使用的扁平Firebase数据结构(如 https://www.firebase.com/docs/android/guide/structuring-data .html ),很难知道导出所需的所有数据何时被收集。



例如:当检索所有使用'索引'(name:key,value true),为每一个我设置一个 addListenerForSingleValueEvent 监听器,但是因为这返回异步,所以不可能确定何时检索所有的索引。这种方式是不可能的,以确定正确的时刻开始出口。



谁有最好的做法来应付这个?

pre> @Override
public void onDataChange(DataSnapshot indexListDataSnapshot){
final long participantsRequired = indexListDataSnapshot.getChildrenCount(); (DataSnapshot ds:indexListDataSnapshot.getChildren()){
DataUtil.getParticipantByKey(mEventKey,ds.getKey()).addListenerForSingleValueEvent(new ValueEventListener(){
@Override(


public void onDataChange(DataSnapshot dataSnapshot){
Participant p = dataSnapshot.getValue(Participant.class);
mParticipants.add(p);

if(participantsRequired == mParticipants.size()){
executeExport();
}
}
$ b $ @覆盖
public void onCancelled(FirebaseError firebaseError){
mListener.onDataLoadFailure(firebaseError.toException());
}
});
}
}


in a Firebase Android app that I'm currently developing I would like to provide an export feature. This feature should allow the user to export a set of data that is stored in Firebase.

My plan is to gather all required data into a intermediate object (datastructure) that can be (re-)used for multiple export types.

I am running into the issue that because of the flat Firebase data structure that I am using (as explained in https://www.firebase.com/docs/android/guide/structuring-data.html), it's difficult to know when all the data required for the export has been collected.

Example: when retrieving all objects that are referenced using 'indices' (name: key, value true), for each of these I set an addListenerForSingleValueEvent listener, but because this returns asynchronous, it's impossible to determine when all the indices are retrieved. This way it's not possible to determine the correct moment to start the export.

Who has best practices for coping with this?

解决方案

Posting this to show a worked out example of the comment of @FrankvanPuffelen that seems to do the job quite well:

@Override
public void onDataChange(DataSnapshot indexListDataSnapshot) {
    final long participantsRequired = indexListDataSnapshot.getChildrenCount();

    for (DataSnapshot ds : indexListDataSnapshot.getChildren()) {
        DataUtil.getParticipantByKey( mEventKey, ds.getKey() ).addListenerForSingleValueEvent( new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Participant p = dataSnapshot.getValue(Participant.class);
                mParticipants.add( p );

                if (participantsRequired == mParticipants.size()){
                    executeExport();
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mListener.onDataLoadFailure( firebaseError.toException() );
            }
        });
    }
}

这篇关于异步收集来自Firebase的数据:数据集何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆