无法删除暂停功能中的演员表

时间:2019-07-01 11:06:53

标签: android retrofit2 kotlin-coroutines

在我的Android应用中,我使用Retrofit 2 lib。

此处api

import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface TraderMonitorRestClient {

    @GET("traders/json")
    suspend fun getTraidersList(): Response<List<Trader>>

    @GET("trader/{trader_operation}")
    suspend fun executeTraderOperation(@Path("trader_operation") traderOperation: String,
                                       @Query("base") base: String,
                                       @Query("quote") quote: String,
                                       @Query("sender") sender: String,
                                       @Query("key") key: String): Response<Void>
}

此处用法:

 suspend fun getTraidersList(isCustomtHandle: Boolean = false): Response<*> {
            suspend fun execOperation(): Response<*> = traderMonitorRestClient.getTraidersList()

            return runOperation(isCustomtHandle, ::execOperation)
        }

        suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String, isCustomtHandle: Boolean = false): Response<*> {
            val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
            val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
            suspend fun execOperation(): Response<*> = traderMonitorRestClient.executeTraderOperation(
                    traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)

            return runOperation(isCustomtHandle, ::execOperation)
}

private suspend fun runOperation(isCustomtHandle: Boolean, func: suspend () -> Response<*>): Response<*> {
            if (isCustomtHandle) {
                return runOperationWithoutHandle(func)
            } else {
                return runOperationWithDefaultHandle(func)
            }
}

suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
            val response: Response<*> = func() // execute sequential http request and in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
            response
}

以及此处的用法:

class TradersViewModel(application: Application) : AndroidViewModel(application) {

 private fun loadData() {
        viewModelScope.launch(Dispatchers.Main) {
            val response: Response<*> = TransportService.getTraidersList()
            if (response.isSuccessful) { // code >= 200 && code < 300;
                finishLoadData()
                val traders: List<Trader> = response.body() as List<Trader>
traderListLiveData.postValue(traders)
}

如您所见,我需要投射

val traders: List<Trader> = response.body() as List<Trader>

好。很好。

但是不想投。所以我试试这个:

@GET("traders/json")
suspend fun getTraidersList(): List<Trader>


suspend fun getTraidersList(isCustomtHandle: Boolean = false): Response<*> {
            suspend fun execOperation(): List<Trader> = traderMonitorRestClient.getTraidersList()

            return runOperation(isCustomtHandle, ::execOperation)
}

但是我在这里遇到编译错误:

return runOperation(isCustomtHandle, ::execOperation)


type mismatch.
Required:
suspend () → Response<*>
Found:
KSuspendFunction0<List<Trader>>

结果必须是这样的:

interface TraderMonitorRestClient {

    @GET("traders/json")
    suspend fun getTraidersList(): List<Trader>

    @GET("trader/{trader_operation}")
    suspend fun executeTraderOperation(@Path("trader_operation") traderOperation: String,
                                       @Query("base") base: String,
                                       @Query("quote") quote: String,
                                       @Query("sender") sender: String,
                                       @Query("key") key: String): Void

0 个答案:

没有答案