为什么我只能在Coroutine主体中调用悬架功能'如果我不添加添加apply {}

发布于 2025-01-30 05:58:11 字数 1723 浏览 0 评论 0原文

有人可以解释为什么我会遇到构建错误

悬架功能只能在Coroutine主体中调用

:: AnothersUspenseFunction时,即以下片段

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {})
    }
}

,当我在呼叫末尾添加空涂抹时,我不会得到它。

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {}).apply {  }
    }
}

这也有效:

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
        responseHandler.call(scope,
          ::anotherSuspenseFunction,
          {
              BaseResultDataModel()
          },
          {})
}

提交签名看起来像这样:

protected suspend fun <T> submit(scope: CoroutineScope, query: suspend () -> NetworkResource<T>): NetworkResource<T>

呼叫签名如下:

    suspend fun <ResponseType, ParsedType : BaseResultDataModel, ConvertedType> call(scope: CoroutineScope,
    query: suspend () -> Response<ResponseType>,
    extractor: suspend (ResponseType) -> ParsedType,
    converter: suspend (ParsedType) -> ConvertedType): NetworkResource<ConvertedType> 

Could someone please explain why I get a build error

Suspension functions can be called only within coroutine body

for ::anotherSuspenseFunction in following snippet

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {})
    }
}

I don't get it when I add an empty apply on the end of call.

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {}).apply {  }
    }
}

This also works:

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
        responseHandler.call(scope,
          ::anotherSuspenseFunction,
          {
              BaseResultDataModel()
          },
          {})
}

Submit signature looks like this:

protected suspend fun <T> submit(scope: CoroutineScope, query: suspend () -> NetworkResource<T>): NetworkResource<T>

Call signature looks like this:

    suspend fun <ResponseType, ParsedType : BaseResultDataModel, ConvertedType> call(scope: CoroutineScope,
    query: suspend () -> Response<ResponseType>,
    extractor: suspend (ResponseType) -> ParsedType,
    converter: suspend (ParsedType) -> ConvertedType): NetworkResource<ConvertedType> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文