如何执行多个Firebase上传并等待汇总结果 [英] How do I execute multiple firebase uploads and wait for the collective results

查看:40
本文介绍了如何执行多个Firebase上传并等待汇总结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Firebase Storage烘焙到一个照片收集应用程序中,该应用程序会收集照片并将其上传到Firebase存储后端.我要上传多张照片,并等待所有任务完成,收集下载网址,然后继续下一步.这是我的下面的代码

I am playing around with Firebase Storage baked into an photo collection app that takes a collection of photos and uploads them to a firebase storage backend. I want to upload multiple photos and wait for all tasks to finish, collect the download urls and proceed to the next step. Here is my code below

    public void saveImageData() {
    Incident incident = new Incident();
    final ArrayList<Uri> downloadUris = new ArrayList<>();

    StorageReference storageRef = storage.getReferenceFromUrl(Constants.FIREBASE_URL);
    for (Uri uri : imageUris) {
        StorageReference imagesRef = storageRef.child("images/" + uri.getLastPathSegment());
        uploadTask = imagesRef.putFile(uri);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                downloadUris.add(taskSnapshot.getDownloadUrl());
                Log.e(TAG, "Size of data: " + downloadUris.size());
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
    }
}

我希望收集所有uri,并使用存储在downloadUris中的结果.在输入时,我意识到这可能不是firebase的问题,而是了解如何等待并行任务完成执行并继续执行结果.我怎样才能实现这个目标?谢谢

I would like for all the uris to be collected and use the results stored in downloadUris. As I type this I realise this may not be a firebase issue but understanding how to wait for parallel tasks to finish executing and proceed with the results. How can I achieve this objective? Thanks

推荐答案

播放服务任务API 实际上为许多Firebase客户端异步API提供了支持,例如Firebase Storage文件上传.您拥有的UploadTask是标准任务的子类接受成功和失败的侦听器,就像您现在正在做的那样.

The Play services Task API actually powers much of the Firebase client side asynchronous APIs, such as Firebase Storage file uploads. That UploadTask you have is a subclass of the standard Task that accepts success and failure listeners, as you're doing it now.

如果您要处理多个并发上传,并希望知道所有上传的完成时间,则可以使用

If you're dealing with multiple concurrent uploads and wish to know when all of them are complete, you can use the Tasks utility class which has a whenAll() method that accepts multiple Task objects. That method will return a new Task where you can register additional success and failure listeners. The success listener will be called as soon as all the Tasks are complete, and the failure listener will be called if and when any of the tasks fail.

您既可以注册各个任务的侦听器,也可以注册Tasks.whenAll()返回的复合任务的侦听器.

You can register listeners both on the individual tasks, and also on the composite task returned from Tasks.whenAll().

(此外,请关注即将发布有关将Task API与Firebase的各种功能结合使用的博客系列.)

这篇关于如何执行多个Firebase上传并等待汇总结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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