Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to observe Ktor download progress by a Flow

I want to observe the download progress by a Flow,
so I wrote a function like this:

suspend fun downloadFile(file: File, url: String): Flow<Int>{
        val client = HttpClient(Android)
        return flow{
            val httpResponse: HttpResponse = client.get(url) {
                onDownload { bytesSentTotal, contentLength ->
                    val progress = (bytesSentTotal * 100f / contentLength).roundToInt()
                    emit(progress)
                }
            }
            val responseBody: ByteArray = httpResponse.receive()
            file.writeBytes(responseBody)
        }
}

but the onDownload will be called only once, and the file will not be downloaded. If I remove the emit(progress) it will work.

io.ktor:ktor-client-android:1.6.7

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use callbackFlow instead of flow. A regular flow can’t launch background code, and can only emit values from code inside the flow itself. Meanwhile, a callback flow can launch other work in the background, and then receive callbacks from it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading