Android 如何在另一个函数中运行挂起函数而不等待其结果?

Android 如何在另一个函数中运行挂起函数而不等待其结果?,android,kotlin-coroutines,coroutine,Android,Kotlin Coroutines,Coroutine,我有一个场景,我的代码必须发送一个api调用并继续工作(其中包含另一个api调用),而不必等待第一个调用的结果 现在我在viewmodel中执行此操作 fun showItem(id:Int) { launch{ repo.markItemRead(id) } launch { try { val item = repo.getItemById(id).getOrThrow commands.postVal

我有一个场景,我的代码必须发送一个api调用并继续工作(其中包含另一个api调用),而不必等待第一个调用的结果

现在我在viewmodel中执行此操作

fun showItem(id:Int) {
   launch{
       repo.markItemRead(id)
   }
   launch {
       try {
           val item = repo.getItemById(id).getOrThrow
           commands.postValue(ShowItemCommand(item))
       } catch (t:Throwable) {
           commands.postValue(ShowError(R.string.error_retrieve_item))
           repo.logError(t)
       }
   }
}
这将调用具有这两个功能的存储库

suspend fun markItemRead(id) {
    try {
        service.markItemAsRead(id)
    } catch(ignored:Throwable) {
    }
}

suspend fun getItemById(id) : Result<ItemData> {
    return try {
       val response : ItemEntity = service.getItemById(id)
       val item  = response.toData()
       Result.Success(item)
    } catch (t:Throwable) {
        Result.Failure(t)
    }
}
suspend-fun-markItemRead(id){
试一试{
service.markItemAsRead(id)
}捕获(忽略:可丢弃){
}
}
suspend fun getItemById(id):结果{
回击{
val响应:ItemEntity=service.getItemById(id)
val item=response.toData()
结果.成功(项目)
}捕获(t:可丢弃){
结果:失败(t)
}
}
我更希望存储库能够完成所有这些工作,因为每次都必须一个接一个

不幸的是,当我试图在存储库中执行类似操作时:

suspend fun getItemById(id:Int) : Result<ItemData> {
    try {
        service.markItemAsRead(id)
    } catch(ignored:Throwable) {
    }
    return try {
       val response : ItemEntity = service.getItemById(id)
       val item  = response.toData()
       Result.Success(item)
    } catch (t:Throwable) {
        Result.Failure(t)
    }
}
suspend fun getItemById(id:Int):结果{
试一试{
service.markItemAsRead(id)
}捕获(忽略:可丢弃){
}
回击{
val响应:ItemEntity=service.getItemById(id)
val item=response.toData()
结果.成功(项目)
}捕获(t:可丢弃){
结果:失败(t)
}
}
它等待
markItemAsRead
函数完成后再继续


除了定义存储库的作用域并将
markItemAsRead
调用放在
launch
中(我已经读到在挂起函数中执行此操作是不正确的)之外,还有其他方法在存储库中执行此操作吗?

您希望并行执行多个任务,但在所有任务完成后返回函数。如果我是对的

您可以使用async/await。内部暂停功能

val d1 = async { t1() }
val d2 = async { t2() }
d1.await()
d2.await()
// all tasks done
t1和t2将并行运行。当t1.wait()调用时,它将等待结果,但t2仍在运行

在您的函数中,您可以这样更改它:

suspend fun getItemById(id:Int) : Result<ItemData> = coroutineScope {
    val t1 = async {
        try {
            service.markItemAsRead(id)
        } catch(ignored:Throwable) {
            null
        }
    }
    val t2 = async {
        try {
            val response : ItemEntity = service.getItemById(id)
            val item = response.toData()
            Result.Success(item)
        } catch (t:Throwable) {
            Result.Failure(t)
        }
    }
    t1.await()
    return@coroutineScope t2.await()
}
suspend fun getItemById(id:Int):结果=coroutineScope{
val t1=异步{
试一试{
service.markItemAsRead(id)
}捕获(忽略:可丢弃){
无效的
}
}
val t2=异步{
试一试{
val响应:ItemEntity=service.getItemById(id)
val item=response.toData()
结果.成功(项目)
}捕获(t:可丢弃){
结果:失败(t)
}
}
t1.等待()
return@coroutineScopet2.等待
}

您希望并行执行多个任务,但在所有任务完成后返回函数。如果我是对的

您可以使用async/await。内部暂停功能

val d1 = async { t1() }
val d2 = async { t2() }
d1.await()
d2.await()
// all tasks done
t1和t2将并行运行。当t1.wait()调用时,它将等待结果,但t2仍在运行

在您的函数中,您可以这样更改它:

suspend fun getItemById(id:Int) : Result<ItemData> = coroutineScope {
    val t1 = async {
        try {
            service.markItemAsRead(id)
        } catch(ignored:Throwable) {
            null
        }
    }
    val t2 = async {
        try {
            val response : ItemEntity = service.getItemById(id)
            val item = response.toData()
            Result.Success(item)
        } catch (t:Throwable) {
            Result.Failure(t)
        }
    }
    t1.await()
    return@coroutineScope t2.await()
}
suspend fun getItemById(id:Int):结果=coroutineScope{
val t1=异步{
试一试{
service.markItemAsRead(id)
}捕获(忽略:可丢弃){
无效的
}
}
val t2=异步{
试一试{
val响应:ItemEntity=service.getItemById(id)
val item=response.toData()
结果.成功(项目)
}捕获(t:可丢弃){
结果:失败(t)
}
}
t1.等待()
return@coroutineScopet2.等待
}

否我希望在不等待响应的情况下运行一个任务,在等待的情况下运行另一个任务,但无论哪种方式,async都需要一个协程作用域,我希望避免必须定义协程作用域或将视图模型的协程作用域传递给我的存储否我希望在不等待响应的情况下运行一个任务,在等待的情况下运行另一个任务,但无论哪种方式,async都需要一个协程作用域,我希望避免必须定义一个协程作用域或将视图模型的协程作用域传递给我的Repository。我认为在这种情况下,我们可以使用launch()。。不确定我想我们可以在这种情况下使用launch()。。不确定