android 如何在exKotlin中逐个同步执行for循环

zte4gxcn  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(92)
private fun sendListOfFeedBack(listOfFeedBack: ArrayList<FeedbackModel>, email: String,
                                   firstName: String,
                                   lastName: String,
                                   uuid: String): Single<String?> {
        if (checkNetworkDataAvailableOrNot()) {
            for (i in 0 until listOfFeedBack.size) {
                // ToDo We have to apply Logic to execute synchronus
                uploadImageAndToFireBase(listOfFeedBack[i],email, firstName, lastName, uuid)
            }
            appPreference.clearAllFeedback()
            return Single.just(REMOTE_SAVED)
        }
        return Single.just(LOCAL_SAVED)
    }

  private fun uploadImageAndToFireBase( feedbackModel: FeedbackModel, email: String, firstName: String, lastName: String, uuid: String ) {
        val listOfImagePath = java.util.ArrayList<String>()
        if (feedbackModel.imageList.size > 0) {
            for (i in 0 until feedbackModel.imageList.size) {
                val ref = storageReference?.child("tests/feedback/images$i.jpg")
                val uploadTask = ref?.putFile(Uri.parse(feedbackModel.imageList[i]))
                val urlTask = uploadTask?.continueWithTask { task ->
                    if (!task.isSuccessful) {
                        task.exception?.let {
                            throw it
                        }
                    }
                    ref.downloadUrl
                }?.addOnCompleteListener { task ->
                    if (task.isSuccessful) {
                        val downloadUri = task.result
                        Log.d("STATUSSS", "Uploaded$downloadUri")
                        listOfImagePath.add(downloadUri.toString())
                        if (feedbackModel.imageList.size == listOfImagePath.size) {
                            uploadDataToFirebaseFeedbackDocument( feedbackModel, listOfImagePath, email, firstName, lastName, uuid )
                        }
                    } else {
                        uploadDataToFirebaseFeedbackDocument( feedbackModel, listOfImagePath, email, firstName, lastName, uuid )
                    }
                }
            }
        } else {
            uploadDataToFirebaseFeedbackDocument( feedbackModel, listOfImagePath, email, firstName, lastName, uuid )
        }
    }

这是我的代码,我试图在循环中上传更多的图像,然后我们将imageUrl发送到firestore数据库,在这种情况下,有时listofimagepath变空,因为在图像上传数据发送到firestore之前,我试图先上传图像,然后我想发送数据,即我想执行同步循环,请帮助我如何同步上传数据到firestore,我正在使用rxkotlin。

xriantvc

xriantvc1#

像这样编写uploadImageAndToFirebase函数,现在应该可以工作了。

private fun uploadImageAndToFireBase( feedbackModel: FeedbackModel, email: String, firstName: String, lastName: String, uuid: String ) {
        val listOfImagePath = java.util.ArrayList<String>()
        if (feedbackModel.imageList.size > 0) {
            for (i in 0 until feedbackModel.imageList.size) {
                val ref = storageReference?.child("tests/feedback/images$i.jpg")
                ref?.putFile(Uri.parse(feedbackModel.imageList[i])).addOnSuccessListener {
                    ref.downloadUrl.addOnSuccessListener {
                        listOfImagePath.add(it.toString)
                        if (feedbackModel.imageList.size == listOfImagePath.size) {
                            uploadDataToFirebaseFeedbackDocument(
                                feedbackModel,
                                listOfImagePath,
                                email,
                                firstName,
                                lastName,
                                uuid
                            )
                        }
                      }
                    }
                }
            }

    }

相关问题