OnSuccess方法内的进度对话框,Firebase-Firestore?

时间:2019-09-26 03:17:09

标签: java android android-studio google-cloud-firestore firebase-storage

我将ArrayList上载到firebase-storage,然后检索下载URL,然后将自定义数据对象存储到firebase-firestore中。我不知道何时停止进度对话框。

        var owners = People.GroupJoin(
            Pets,
            person => new { person.Id, person.Age },
            pet => new { pet.Id, Age = pet.Age * 2 },
            (person, pet) => new
            {
                Person = person,
                Pets = pet,
            }).SelectMany(
            pet => pet.Pets.DefaultIfEmpty(),
            (people, pet) => new
            {
                people.Person,
                Pet = pet,
            });

1 个答案:

答案 0 :(得分:1)

reference.putFile(file)异步上传文件,因此请不要在for循环中上传所有文件。

创建一个上传文件的功能,而不是在onSuccess中成功上传文件时,从列表中删除上传的文件对象,然后发送下一个要上传的文件对象。

当文件列表dismiss中没有文件时,对话框。

这就像一个队列。

private ProgressDialog dialog;
private ArrayList<NoteItem> nItems

public void uploadNotesToSTORAGE(final ArrayList<NoteItem> items, Activity activity) {
    nItems = items

    dialog = new ProgressDialog(activity);
    dialog.setMessage("Uploading picture...");
    dialog.show();


    if(nItems != null && !nItems.isEmpty()){
      uploadSingleNote(nItems.get(0));        //get first item
     }
}

private void uploadSingleNote(NoteItem item){
    final String id = Calendar.getInstance().getTimeInMillis() + "";
    StorageReference reference = mStorageRef.child("notes/" + id);
    String filePath = item.getFilePath();

    reference.putFile(Uri.fromFile(new File(filePath))).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            StorageReference reference = mStorageRef.child("notes/" + id);
            reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri dUri) {
                    item.setId(id);
                    item.setDownloadLink(dUri + "");
                    db.collection("notes").document().set(nItems.get(finalI))
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(activity, "Successfully uploaded : " + item.getFileName(), Toast.LENGTH_SHORT).show();
                            nItems.remove(0);
                            if(nItems != null && !nItems.isEmpty()){
                               uploadSingleNote(nItems.get(0));        //get first item
                             }else{
                              dialog.dismiss();
                                activity.finish();
                              activity.startActivity(new Intent(activity, MainActivity.class));
                             }
                          }
                     });
                }
            });
        }
    }).addOnFailureListener(onFailureListener);
}