skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    I fixed the problem with the help of a callback that will be activated as soon as the function ends and I won't have to do an await operation


  2. Use coroutine like this

    suspend fun uploadImageFile(): String {
        return withContext(Dispatchers.IO) {
            val deferredResult = async {               
                uploadImage()
            }
            deferredResult.await()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search