How to execute for loop one by one synchronous in ex kotlin

88 Views Asked by At
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 )
        }
    }

This is my code i am trying to upload more image in loop then we are sending imageUrl to firestore data base what happen in this case some time listofimagepath getting empty because before image uploading data send to firestore i am trying to upload image first then i want to send data i.e i want execute synchronusly loop can you please help me how to upload data to firestore synchronous i am using rxkotlin .

1

There are 1 best solutions below

4
zaid khan On

Write your uploadImageAndToFirebase function like this and it should work now.

    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
                            )
                        }
                      }
                    }
                }
            }

    }