Răzvan Barbu
Răzvan Barbu

Reputation: 189

Chain Requests Kotlin Coroutines

I have an array of URLs, each providing a zip file. I want to download them and store them in my app folders, inside the internal memory.

Question:

Since I do not know the number of URLs I will need to access, what is the best way to go about this? I am just beginning to work with Kotlin coroutines.

This is my 'download from url' method

fun downloadResourceArchiveFromUrl(urlString: String, context: Context): Boolean {

    Timber.d("-> Started downloading resource archive.. $urlString")
    lateinit var file: File
    try {
        val url = URL(urlString)

        val urlConn = url.openConnection()
        urlConn.readTimeout = 5000
        urlConn.connectTimeout = 10000

        val inputStream = urlConn.getInputStream()
        val buffInStream = BufferedInputStream(inputStream, 1024 * 5)

        val fileNameFromUrl = urlString.substringAfterLast("/")

        file = File(context.getDir("resources", Context.MODE_PRIVATE) , fileNameFromUrl)

        val outStream = FileOutputStream(file)
        val buff = ByteArray(5 * 1024)

        while (buffInStream.read(buff)  != -1){
            outStream.write(buff, 0, buffInStream.read(buff))
        }

        outStream.flush()
        outStream.close()
        buffInStream.close()

    } catch (e: Exception) {
        e.printStackTrace()
        Timber.d("Download finished with exception: ${e.message} -<")
        return false
    }

    Timber.d("Download finished -<")
    return true
}

Could you simply create a loop and call download method each time?

        for (i in resources.indices) {
            asyncAwait {
                downloadResourcesFromUrl(resources[i].url, context)
                return@asyncAwait
            }

Also, is it a good idea to do this synchronously? Wait for every file to download then proceed to the next one?

Upvotes: 5

Views: 4746

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200168

Turn your blocking download function into a suspending one:

suspend fun downloadResourceArchiveFromUrl(
    urlString: String, context: Context
): Boolean = withContext(Dispatchers.IO) {
   ... your function body
}

Now run your loop inside a coroutine you launch:

myActivity.launch {
    resources.forEach {
        val success = downloadResourceArchiveFromUrl(it.url, context)
        ... react to success/failure ...
    }
}

Also be sure to properly implement structured concurrency on your activity.

Upvotes: 1

Related Questions