How to: fire and forget async coroutines in Kotlin

I've been reading up on Kotlin co-routines but am not finding the answer to a specific problem.

Say I want to iterate over a collection making an API call for each element (in this particular case pushing a file to Amazon S3). I want these calls to be handled by an async coroutine so as not to block the underlying thread while waiting.

I do not need a return value from the request, only to log exceptions.

How would I create a "fire and forget" async coroutine to make one of these requests?


maybe kotlinx.coroutines#launch or kotlinx.coroutines#async meet your needs. for examples:

launch(CommonPool) {
    for(item in collection){
      val result = apiCall(item);
      log(result);
    }
}

OR

for(item in collection){
    launch(CommonPool) {
      val result = apiCall(item)
      log(result)
    }
}
链接地址: http://www.djcxy.com/p/53258.html

上一篇: kotlin协同程序是否使用定时器进行异步调用?

下一篇: 如何:在Kotlin中触发并忘记异步协程