简体   繁体   English

使用 Jetpack Compose Paging3,在 Launch Effect 块中的分页项目上调用 refresh() 不会更新 UI

[英]With Jetpack Compose Paging3, calling refresh() on paging items in Launch Effect block doesn't update the UI

I've applied Paging3 compose with my Android Jetpack Compose project.我已经在我的 Android Jetpack Compose 项目中应用了 Paging3 compose。 In one of my screens, the following code is implemented to refresh the list on navigating back from another screen.在我的一个屏幕中,实现了以下代码以在从另一个屏幕导航回来时刷新列表。

LaunchEffect(key1 = Unit) {
      items.refresh()
}

The initial recomposition on navigation back indeed triggers the lambda block in Launch Effect but doesn't reschedule the recomposition again to update the LazyColumn.返回导航时的初始重组确实会触发 Launch Effect 中的 lambda 块,但不会再次重新安排重组以更新 LazyColumn。

LazyColumn(
     modifier = Modifier.fillMaxSize()
) {
     item { Spacer(modifier = Modifier.height(14.dp)) }
     items(items) { item ->
          Item(blog = item!!)
     }
}

Please suggest me a solution to fix this.请建议我解决此问题的解决方案。 Right now, I've removed cacheIn() in view model just to force pager to refetch new objects but that'll bring back to page 1, apparently not how it's supposed to be.现在,我已经删除了视图 model 中的 cacheIn() 只是为了强制寻呼机重新获取新对象,但这将带回第 1 页,显然不是它应该的样子。 Also, if we wrap the lazy column with Swipe Refresh and manually refresh, bothe the data and UI got updated.此外,如果我们用滑动刷新包裹惰性列并手动刷新,数据和 UI 都会更新。 Any solution for this?有什么解决办法吗?

I had the same problem as you, and after experimenting I found that there was some differences reg coroutine calls when using LaunchEffect vs fx SwipeToRefresh calls to items.refresh().我遇到了和你一样的问题,经过试验我发现在使用 LaunchEffect 时 reg 协程调用与 fx SwipeToRefresh 调用 items.refresh() 时存在一些差异。

So I tried this and it worked for me:所以我尝试了这个并且它对我有用:

LaunchEffect(key1 = Unit) {
    launch(Dispatchers.Main) {
        items.refresh()
    }
}

Exactly why this works and if there are any unwanted side-effects I don't know.究竟为什么会这样,以及是否有任何我不知道的副作用。

Bonus info from my small experiment:我的小实验的奖励信息:

  • Dispatchers.Default also worked. Dispatchers.Default也有效。
  • Dispatchers.IO worked sometimes . Dispatchers.IO有时工作。
  • Dispatchers.Unconfined had no effect. Dispatchers.Unconfined无效。
  • launch{...} completely invalidated my list (items disappeared while loading) launch{...}使我的列表完全无效(加载时项目消失了)
  • launch(Job()){...} completely invalidated my list (items disappeared while loading) launch(Job()){...}使我的列表完全无效(加载时项目消失了)
  • launch(newSingleThreadContext("MyOwnThread")){...} worked sometimes . launch(newSingleThreadContext("MyOwnThread")){...}有时工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM