【问题标题】:Stuck with Api response Ktor卡住 Api 响应 Ktor
【发布时间】:2021-05-15 11:38:14
【问题描述】:

我正在尝试使用 Ktor 为我们的 ApiServices 构建 KMM 应用程序。我创建了一个BaseApiClass,其中包含所有与 api 相关的代码。

BaseApiClass 的代码:-

class BaseAPIClass {

//Create Http Client
private val httpClient by lazy {
    HttpClient {
        defaultRequest {
            host = ApiEndPoints.Base.url
            contentType(ContentType.Application.Json)
            header(CONNECTION, CLOSE)
        }
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.ALL
        }
        install(HttpTimeout) {
            requestTimeoutMillis = NETWORK_REQUEST_TIMEOUT
        }
        expectSuccess = false
        // JSON Deserializer
        install(JsonFeature) {
            val json = Json {
                ignoreUnknownKeys = true
                coerceInputValues = true
            }
            serializer = KotlinxSerializer(json)
        }
    }
}


// Api Calling Functions I have few more similar to this but issue is random and comes in any of the api
@Throws(Exception::class)
suspend fun sampleApi(requestBody: RequestBody?) : Either<CustomException, BaseResponse<EmptyResponseModel>> {
    return try {
        val response = httpClient.post<BaseResponse<EmptyResponseModel>> {
            url(ApiEndPoints.sample.url)
            if (requestBody != null) {
                body = requestBody
            }
        }
        Success(response)
    }
    catch (e: Exception) {
        Failure(e as CustomException)
    }
}

这是我从 iOS 应用程序调用 api 的方式:-

val apiClass = BaseApiClass()

func callApi() {
        apiClass.sampleApi(requestBody: .init(string: "value here")) { (result, error) in
            result?.fold(failed: { (error) -> Any? in
                // Error here 
            }, succeeded: { (result) -> Any? in
                // Success here 
            })
        }
    }

现在在这里,如果我尝试使用相同的 objectapiClass 调用类似的更多 api,那么在几次调用之后它就会卡在我的函数 callApi 中,它甚至不会发送 api 请求(因为我可以'看不到打印在我的控制台中的请求日志),因此我无法执行任何其他操作,因为我没有从 api 获得任何东西。

只要我更改屏幕或关闭应用程序并尝试调用相同的 api,它就可以正常工作。

但是,如果我尝试使用BaseApiClass().sampleApi(request params here) {// completion handler here},而不是像 apiClass = BaseApiClass() 那样只创建一个对象,它可以正常工作,我对此没有任何问题。

我不确定是什么导致了这种情况发生在Android 中一切正常,只有iOS 面临这种情况。

【问题讨论】:

标签: ios swift kotlin-multiplatform ktor kotlin-multiplatform-mobile


【解决方案1】:

尝试在 install(Logging) 块中设置 LogLevel.NONE
目前我以这种方式解决,因为它似乎是 Ktor 的一个错误。
见:https://youtrack.jetbrains.com/issue/KTOR-2711
1.6.0版本应该已经修复了。

【讨论】:

  • 看起来问题在 1.6.7 中仍然存在。无论如何,这为我解决了类似的问题。
  • 是的,Ktor 团队对我说,这个卡住的问题只能在应该很快发布的 2.0.0 版本中解决。补丁版本中没有修复。见:youtrack.jetbrains.com/issue/KTOR-2986
  • 感谢您的链接!祈祷我们可以使用 2.0 再次启用日志记录。
【解决方案2】:

您是否使用协程库的多线程变体?官方文档声明您在使用 Ktor 时应该使用此变体。见here

【讨论】:

    【解决方案3】:

    经过所有努力并尝试了很多调试技巧,我了解到即使我收到来自 api 的响应,也永远不会调用共享模块中的完成处理程序。

    我实现的唯一解决方案是使用expectactual 机制创建不同的HTTP 客户端。通过创建单独的客户,我还没有遇到这个问题。

    如果您有任何其他答案或解决方案,我很乐意看看。

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多