2

I have a list of urls in a mutable list and I want to perform an IO operation, cacheVideo on each of the urls sequentially one after the other

suspend fun cacheVideo(mediaItem: MediaItem) = {
    val videoUrl = mediaItem.mediaUrl
    val uri = Uri.parse(videoUrl)
    val dataSpec = DataSpec(uri)

    val progressListener =
        CacheUtil.ProgressListener { requestLength, bytesCached, newBytesCached ->

            val downloadPercentage: Double = (bytesCached * 100.0
                    / requestLength)
            
            if (downloadPercentage == 100.0) {
                // I WANT TO RETURN HERE
            }
        }

    try {
        CacheUtil.cache(
            dataSpec,
            cache,
            DataSourceFactory?.createDataSource(),
            progressListener,
            null
        );
    } catch (err: Exception) {
        // IF ERROR, THEN RETURN NULL
    }
}

How would I shape the cacheVideo to do that using Coroutines?

uiScope.launch {
 for(item in mediaItems){
  cacheVideo(item) // I WANT TO WAIT HERE BEFORE GOING TO NEXT ITEM
 }
}
1
  • You can create an interface callback to get when previous work is done. Commented Jul 10, 2020 at 9:25

1 Answer 1

2

You can use suspendCancellableCoroutine to wait for the progress:

suspend fun cacheVideo(mediaItem: MediaItem) = suspendCancellableCoroutine { continuation ->
    val videoUrl = mediaItem.mediaUrl
    val uri = Uri.parse(videoUrl)
    val dataSpec = DataSpec(uri)

    val progressListener =
        CacheUtil.ProgressListener { requestLength, bytesCached, newBytesCached ->

            val downloadPercentage: Double = (bytesCached * 100.0
                    / requestLength)
            
            if (downloadPercentage == 100.0) {
                continuation.resume() // resumes the execution of the corresponding coroutine 
            }
        }

//  continuation.invokeOnCancellation {
//      // clear some resources, cancel tasks, close streams etc if need.
//  }

    try {
        CacheUtil.cache(
            dataSpec,
            cache,
            DataSourceFactory?.createDataSource(),
            progressListener,
            null
        );
    } catch (err: Exception) {
        continuation.resume() // resumes the execution of the corresponding coroutine 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.