异步任务跟不上 for 循环 (firebase)

Async task could not keep up with for loop (firebase)

  for (Uri mUri : mSelected) 
   {
       imagesRef = storageRef.child(postid + mUri.getLastPathSegment());
       imagesRef.putFile(mUri).addOnSuccessListener(task4 ->
       db.collection("posts").document(postid).update("photo_id", FieldValue.arrayUnion(imagesRef.getDownloadUrl())));
   }

所以,现在我正在使用 firestore 和 firebase 存储。我将(多个)图像上传到存储,上传后,我得到了下载 url 并想将它添加到我的 firestore 中。所以,问题是,只有最后一个图像被添加到 firestore 中,所有图像都被添加到存储中,但只有最后一个 imgUrl 被添加到 firestore 中。由于 firebase 使用异步任务,我怀疑这是因为更新任务跟不上循环。非常感谢任何帮助!!!

我认为问题可能出在 imagesRef,它是在 for (Uri mUri : mSelected) {...} 之外声明的,因此在 addOnSuccessListener(...) 响应之前被替换。

所以,在本地向 for (Uri mUri : mSelected) {...} 声明它,看看它是否能解决问题。像这样

for (Uri mUri : mSelected) 
{
       var imagesRef = storageRef.child(postid + mUri.getLastPathSegment());
       imagesRef.putFile(mUri).addOnSuccessListener(task4 ->
       db.collection("posts").document(postid).update("photo_id", FieldValue.arrayUnion(imagesRef.getDownloadUrl())));
 }