Kotlin彼此结合了两个回击流

发布于 2025-02-12 13:05:49 字数 2195 浏览 0 评论 0原文

我有数据类,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以这样的方式函数,它使我可以组合值mySectionlist&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 技术交流群。

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

发布评论

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