有人可以在成功侦听器上提供Firebase的逻辑帮助我吗 [英] Can someone help me with logic of the firebase on success listener

查看:38
本文介绍了有人可以在成功侦听器上提供Firebase的逻辑帮助我吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Firebase数据库添加和检索图像.上载图像后,该图像将显示在recyclerview的另一个活动中.当添加多个图像时,活动会一直在循环中打开,即取决于添加的图像数量.我想在成功添加所有图像后立即打开所有活动.我尝试制作一种将int与 mClipData.getItemCount()进行比较的逻辑,但是它没有给出任何解决方案请帮助我解决这个问题

I am adding and retrieving images from firebase database. When an image is uploaded, it is displayed on another activity in recyclerview. When adding multiple images, then activity keeps opening in the loop i.e dependent on how many images are added. I want to open all activities at once when all images are successfully added. I tried making a logic in which an int a is compared with the mClipData.getItemCount() but it doesn't give any solution kindly help me with this problem

PS :给出的代码用于多张图片

P.S: the code given is for multiple images

    if (data.getClipData() != null) {
        ClipData mClipData = data.getClipData();
        ArrayList<Uri> mArrayUri = new ArrayList<>();
        for (int i = 0; i < mClipData.getItemCount(); i++) {

            final ClipData.Item item = mClipData.getItemAt(i);
            final Uri mImageUri = item.getUri();
            mArrayUri.add(mImageUri);
            // Get the cursor
            Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            final StorageReference profileStorage = uploadStorage.child(mImageUri.getLastPathSegment());
            uploadTask = profileStorage.putFile(mImageUri);
            final int finalI = i;
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    showMessage(e.toString());
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                            if (!task.isSuccessful()){
                                throw task.getException();
                            }
                            downloadURL = profileStorage.getDownloadUrl().toString();
                            return profileStorage.getDownloadUrl();
                        }
                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            if(task.isSuccessful()){
                                Uri uri = task.getResult();
                                idRef.child(mImageUri.getLastPathSegment()).setValue(uri.toString());
                            }
                        }
                    }).addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            showMessage("image upload successful ");
                            // activity opens multiple times check it //
                            a++;
                        }
                    });
                }
            });

            if(a == mClipData.getItemCount()){
                Intent intent = new Intent(getApplicationContext(), DisplayImages.class);
                startActivity(intent);
            }

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imageEncoded  = cursor.getString(columnIndex);
            imagesEncodedList.add(imageEncoded);
            cursor.close();
        }

    }

推荐答案

上载图像时,每个图像都会发生以下顺序:

When you upload the images, the following sequence happens for each:

  1. 您开始上传.
  2. 完成后,您将确定图像的下载URL.
  3. 完成后,您将下载URL写入数据库.

每个操作都是异步的,您可以通过任务进行处理.由于您希望仅在所有下载URL都写入数据库后才显示弹出窗口,因此代码应位于最后一个完成处理程序中:

Each of these operations is asynchronous, which you handle via tasks. Since you want the popup to only be shown once all download URLs are written to the database, the code should be in the last completion handler:

}).addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        showMessage("image upload successful ");
        // activity opens multiple times check it //
        a++;

        if(a == mClipData.getItemCount()){
            Intent intent = new Intent(getApplicationContext(), DisplayImages.class);
            startActivity(intent);
        }
    }
});

这篇关于有人可以在成功侦听器上提供Firebase的逻辑帮助我吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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