类型推断失败。预期的类型不匹配:找到了Response <Void> Deferred <Response <Void >>

时间:2019-06-12 15:22:58

标签: android kotlin-coroutines

在我的Android应用中

import kotlinx.coroutines.*
import retrofit2.Response
import java.util.*

   fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String): Response<Void> {
            lateinit var executeOperations: Response<Void>
            GlobalScope.launch(Dispatchers.IO) {
                executeOperations = async { runOperation(traderOperation, base, quote) }
            }
            return executeOperations
        }

        suspend fun runOperation(traderOperation: Trader.Operation, base: String, quote: String): Response<Void> {
            val traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient::class.java)
            val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
            val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
            val executeTraderOperation = traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
            val response = executeTraderOperation.await()
            return response
}

但是在这一行中我得到编译错误:

executeOperations = async { runOperation(traderOperation, base, quote) }

错误消息:

Type inference failed. Expected type mismatch: 
required:
Response<Void>
found:
Deferred<Response<Void>>

1 个答案:

答案 0 :(得分:0)

async返回该Deffered,将其删除并将其转换为:

async{
  val executions = runOperation(traderOperation, base, quote)
}
相关问题