即使我没有CallbackListener,我也可以使用回调流吗
我正在使用GraphQl从服务器获取数据。 SDK并非暂停,我正在尝试使用干净的体系结构。因此,我来到我的存储库中返回Flow
,以允许用途服务器的响应。因此,我的存储库中有此代码:
override fun singUp(model: SignUpModel): Flow<ResponseModel<SignUpServerResponse>> = callbackFlow {
...
client.mutateGraph(signUpQuery)
.enqueue(object : GraphCallResultCallback<Storefront.Mutation> {
override fun invoke(result: GraphCallResult<Storefront.Mutation>) {
if (result is GraphCallResult.Success)
trySendBlocking(...).also { close() }
else
trySendBlocking(...).also { close() }
}
})
awaitClose {}
}
我的问题是:
1-每个事件后我需要致电关闭吗?显然,在从服务器中获取响应后,我不需要流动。
2-我删除了等待{}
,并且我发现需要此字段,并且应使用该字段来分离callbackListener,但是在这种情况下,我没有一个。如果我最后放置一个空的等待{}
,可以吗?
3-我知道该流程在后台使用通道。我还通过频道实施了它,我必须使用回调流吗?
I'm using GraphQL to fetch the data from the server. The SDK is not suspendable, and I'm trying to use Clean Architecture. So I came up to return a Flow
in my repository to allow the UseCases to fetch the server's response. So I have this code in my repository:
override fun singUp(model: SignUpModel): Flow<ResponseModel<SignUpServerResponse>> = callbackFlow {
...
client.mutateGraph(signUpQuery)
.enqueue(object : GraphCallResultCallback<Storefront.Mutation> {
override fun invoke(result: GraphCallResult<Storefront.Mutation>) {
if (result is GraphCallResult.Success)
trySendBlocking(...).also { close() }
else
trySendBlocking(...).also { close() }
}
})
awaitClose {}
}
my questions are:
1- Do I need to call the close after each event? Obviously, I don't need the flow to be alive after I fetch the response from the server.
2- I removed awaitClose {}
, and I got the error that this field is required and should be used to detach the CallBackListener, but in this case, I don't have one. Is it okay if I'm putting an empty awaitClose {}
at the end?
3- I know the flow uses Channels in the background. I also implemented it by channels, is there any advantages ( performance, security, etc.) that I have to using CallBackFlow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在此处使用
callbackflow
,就如注释中提到的那样。如果要使用coroutines使用singup()
方法,则需要将其转换为暂停
函数。要将带回调的函数转换为暂停
函数suppendCoroutine
和suppendCancellableCoroutine
可以使用构建器。例子:You don't need to use
callbackFlow
here like mentioned in the comments. If you want to usesingUp()
method with coroutines you need to convert it to asuspend
function. To convert a function, that has callbacks, to asuspend
functionsuspendCoroutine
andsuspendCancellableCoroutine
builders can be used. Example: