为什么我只能在Coroutine主体中调用悬架功能'如果我不添加添加apply {}
有人可以解释为什么我会遇到构建错误
悬架功能只能在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论