0
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 .

MARSH
  • 117
  • 1
  • 7
  • 22

1 Answers1

0

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

    }
zaid khan
  • 825
  • 3
  • 11
  • NO in Feedback model we have 2 to 3 image first we have to download image url after uploading then we have to send all 3 url with single feedback – MARSH Jun 02 '23 at 04:37
  • @MARSH that's what i have done, after adding the url's to the list there is a check if the size list of images equals the size of download url then only call the upload to firestore function – zaid khan Jun 02 '23 at 04:41
  • Thanks for helping me out but what we have List so suppose we have 3 feedback in that list then each feedback contain 3 image so we have first apply loop on feedback then each feedback image to upload so we have to send 3 feedback with contains 3-3 image hope you understand what i am trying to do . – MARSH Jun 02 '23 at 05:14
  • @MARSH for that you could apply a for each loop to the List inside the for each loop there would be a nested for loop which will iterate over the list of images inside each of the model , during the nested iteration you can save the image the way I did in the answer, hope so you understood please upvote and accept if I have helped you :) – zaid khan Jun 02 '23 at 07:25