October 22, 2024
Chicago 12, Melborne City, USA
Android

how to wait until the function is complete in kotlin?


I have the function download url and I want the main program to wait until I have a suitable url and I can put it in the object

    fun uploadImage(userName: String, uri: Uri): String
    {
        uploadImageProcess(userName , uri)
        return downloadUri
    }
     private fun uploadImageProcess(userName: String, uri: Uri){
        val ref = createReference(userName)
        Log.d("uploadImageFUNCTION", "Reference: $ref")
        val uploadTask = ref.putFile(uri)
        val urlTask = uploadTask.continueWithTask { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    Log.d("uploadImageFUNCTION", "task upload .exception: $task.exception")
                    throw it
                }
            }
            ref.downloadUrl
        }.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Log.d("uploadImageFUNCTION", "task downloadUrl : ${task.result}")
                downloadUri = task.result.toString()
            } else {
                Log.d("uploadImageFUNCTION", "task downloadUrl .exception: $task.exception")
            }
        }

    }

plz help me

I’m trying a async but no luck



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video