Kotlin彼此结合了两个回击流
我有数据类,mySection
,其中包含ID。我有另一个数据类myarticle
不在乎其内容
以获取该部分,
private fun getSectionFlow(): Flow<List<MySection>> =
callbackFlow {
val listCallBackFlow = object : SectionCallback<List<MySection>>() {
override fun onSuccess(sections: List<MySection>) {
trySend(sections)
}
override fun onError(errorResponse: ErrorResponse) {
close(Exception(errorResponse.reason))
}
}
provider.getSections(
listCallBackFlow
)
awaitClose()
}
最后
private fun getArticleFlow(id: Long): Flow<List<MyArticle>> =
callbackFlow {
val listCallBackFlow = object : ArticleCallback<List<MyArticle>>() {
override fun onSuccess(sections: List<MyArticle>) {
trySend(sections)
}
override fun onError(errorResponse: ErrorResponse) {
close(Exception(errorResponse.reason))
}
}
provider.getArticle(
id, // to get the article list it needs section id
listCallBackFlow
)
awaitClose()
}
,所有值都需要在list&lt; mySection -Witharticle&gt;
data class MySectionWithArticle (
val id: Long,
val title: String,
var myArticlesList: List<MyArticle> = emptyList()
)
因此,输入到getArticleFlow(id)
应该是getsectionflow()
的值的ID,
因此主要问题是如何组合两个CallbackFlow以这样的方式函数,它使我可以组合值mySection
,list&lt; myarticle&gt;
to mySection Witharticle
?
因此,对于list&lt; mySection&gt;
,每个mySection
需要调用getArticleFlow(id)
函数,然后list&lt; myarticle&gt;
与mySectionWitharticle
数据类结合使用?
如何实现此功能?
I have data class, MySection
, which has the ID in it. I have another data class MyArticle
whdoesn'ton't care about its content
To get the Section,
private fun getSectionFlow(): Flow<List<MySection>> =
callbackFlow {
val listCallBackFlow = object : SectionCallback<List<MySection>>() {
override fun onSuccess(sections: List<MySection>) {
trySend(sections)
}
override fun onError(errorResponse: ErrorResponse) {
close(Exception(errorResponse.reason))
}
}
provider.getSections(
listCallBackFlow
)
awaitClose()
}
And for Article,
private fun getArticleFlow(id: Long): Flow<List<MyArticle>> =
callbackFlow {
val listCallBackFlow = object : ArticleCallback<List<MyArticle>>() {
override fun onSuccess(sections: List<MyArticle>) {
trySend(sections)
}
override fun onError(errorResponse: ErrorResponse) {
close(Exception(errorResponse.reason))
}
}
provider.getArticle(
id, // to get the article list it needs section id
listCallBackFlow
)
awaitClose()
}
And finally, all values need to bind in List<MySectionWithArticle>
data class MySectionWithArticle (
val id: Long,
val title: String,
var myArticlesList: List<MyArticle> = emptyList()
)
So the inputs to getArticleFlow(id)
should be the IDs of the values from getSectionFlow()
So the main question is how can I combine two callbackflow functions in such a way it gives me combine values MySection
,List<MyArticle>
to MySectionWithArticle
?
So for List<MySection>
, each MySection
need to call getArticleFlow(id)
function and then List<MyArticle>
combine with MySectionWithArticle
data class
How can I achieve this functionality ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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