You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tasks.whenAllSuccess失效:批量年费支付数据未写入数据库

问题分析与修复方案

核心问题

你的代码存在三个关键问题,导致数据未写入数据库但Toast错误提示"Added":

  1. 最后一批数据未提交
    循环中仅当counter == 499时才提交当前批次并创建新批次,但如果选中成员数量不是499的整数倍,最后一批数据不会被加入tasks执行提交操作。

  2. 字段名语法错误
    代码中map.put("year", ...)的键名包含多余反引号,正确写法应为"year"`,这个错误会导致字段写入异常。

  3. 空任务列表的误判
    若selectList为空或选中成员数少于499,tasks列表会是空的,Tasks.whenAllSuccess(tasks)会直接判定为成功,触发"Added"提示但无实际写入操作。

修复后的代码

private void pay(ActionMode mode) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Confirm Annual Due")
            .setMessage("Please confirm to add annual due")
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    progressDialog.setTitle("Adding New Annual Due");
                    progressDialog.setMessage("Please wait...");
                    progressDialog.setCancelable(false);
                    progressDialog.create();
                    progressDialog.show();

                    List<WriteBatch> writeBatches = new ArrayList<>();
                    List<Task<Void>> tasks = new ArrayList<>();
                    writeBatches.add(db.batch());
                    int counter = 0, batchIndex = 0;

                    for(Faculty faculty : selectList){
                        Map<String, Object> map = new HashMap<>();
                        map.put("facultyId", faculty.getId());
                        map.put("photoUrl", faculty.getPhotoUrl());
                        map.put("fullName", faculty.getFullName());
                        map.put("campus", faculty.getCampus());
                        map.put("acadRank", faculty.getAcadRank());
                        map.put("position", faculty.getPosition());
                        map.put("memberId", faculty.getMemberId());
                        map.put("acctType", faculty.getAcctType());
                        map.put("acctStatus", faculty.getAcctStatus());
                        map.put("amtPaid", String.valueOf(ANN_DUE_AMT));
                        map.put("datePaid", LocalDate.now().toString());
                        // 修复字段名错误:移除多余反引号
                        map.put("year", String.valueOf(LocalDate.now().getYear()));
                        map.put("status", "Pending");

                        map.values().removeAll(Collections.singleton(null));
                        writeBatches.get(batchIndex).set(annDueRef.document(), map);
                        counter++;

                        if(counter == 499){
                            tasks.add(writeBatches.get(batchIndex).commit());
                            writeBatches.add(db.batch());
                            counter = 0;
                            batchIndex++;
                        }
                    }

                    // 提交最后一批未完成的数据
                    if(counter > 0){
                        tasks.add(writeBatches.get(batchIndex).commit());
                    }

                    // 处理空任务列表的情况
                    if(tasks.isEmpty()){
                        progressDialog.dismiss();
                        Toast.makeText(context, "No members selected", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Tasks.whenAllSuccess(tasks)
                            .addOnCompleteListener(new OnCompleteListener<List<Object>>() {
                                @Override
                                public void onComplete(@NonNull @NotNull Task<List<Object>> task) {
                                    progressDialog.dismiss();

                                    if(task.isSuccessful()){
                                        Toast.makeText(context, "Added", Toast.LENGTH_SHORT).show();
                                        notifyDataSetChanged();
                                        selectList.clear();
                                        mode.finish();
                                    }else{
                                        Toast.makeText(context, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            })
            .setNegativeButton("Cancel", null)
            .create().show();
}

修复说明

  • 修正year字段名错误,改为合法的year`。
  • 循环结束后新增判断,提交未达到499条的最后一批数据。
  • 增加空任务列表判断,避免选中成员为空时错误提示"Added"。

内容的提问来源于stack exchange,提问作者Vince Ybañez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 17:15:37