惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
N
Netflix TechBlog - Medium
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
G
Google Developers Blog
Recorded Future
Recorded Future
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
G
GRAHAM CLULEY
A
Arctic Wolf
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Visual Studio Blog
Webroot Blog
Webroot Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
H
Heimdal Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
M
MIT News - Artificial intelligence
V2EX - 技术
V2EX - 技术
Jina AI
Jina AI
TaoSecurity Blog
TaoSecurity Blog
NISL@THU
NISL@THU
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
WordPress大学
WordPress大学
V
V2EX
Cyberwarzone
Cyberwarzone
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - SharpCJ

理解 Agent 中的 Slash Command:从概念到自定义命令实践 Harness Engineering:把 AI 真正接进工程流程 当 AI 开始写代码,谁来保证它不会翻车? Android 开发者为什么必须掌握 AI 能力?端侧视角下的技术变革 拆穿名词诈骗!用大白话理解晦涩难懂的AI概念 OpenClaw 大结局——接入个人微信 Android 性能分析工具 Perfetto 的基本使用 frp 内网穿透 0.63.0 教程 LangChain入门学习 Ubuntu 下 conda 设置 ComfyUI 基础教程(六) —— 图像的局部重绘 ComfyUI 基础教程(五) —— 应用 IP-Adapter 实现图像风格迁移 ComfyUI 基础教程(四) —— 应用 LoRA 模型控制图像生成特征 ComfyUI 基础教程(三) —— 应用 Controlnet 精准控制图像生成 ComfyUI 基础教程(二) —— Stable Diffusion 文生图基础工作流及模型、常用节点介绍 ComfyUI 基础教程(一) —— 本地安装部署 Stable Diffusion 小白的入坑铺垫 GPT-SoVITS —— 5s 声音样本就可以训练模型,复刻声音的 AI 应用 LM Studio + open-webui 快速本地部署大语言模型 Jetpack Compose(9)——自定义Composable Jetpack Compose(8)——嵌套滚动 Jetpack Compose(7)——触摸反馈
Compose 延迟列表踩过的坑
SharpCJ · 2024-06-27 · via 博客园 - SharpCJ

问题

在使用 Jetpack Compose 延迟列表时遇到一个坑,简单记录一下。直接上代码:


这个代码看起来也没有什么问题,滑动正常,点击滑动到顶部也正常。
但是极端操作:在一边滑动列表一边点击按钮,就出问题了。这样再点击按钮,就不生效了。从日志来看,点击时协程发射值没有问题,但是 collect 不执行了。

如果直接在点击事件中启动协程,执行操作,就不会有问题。

Button(onClick = {
    scope.launch {
        state.animateScrollToItem(0)
    }
}){

}

那问题出现在哪里了呢?

实际上是,在点击按钮是,执行滚动的动画,同时,手滑列表,会抛出动画中断的异常,没有处理的话,会把导致 flow 的 collect 出现问题。
解决办法:

LaunchedEffect(Unit) {
    viewModel.scrollToTop.collect {
        runCatching {
            state.animateScrollToItem(0)
        }
    }
}

总结

最后总结一下,这个问题类似的情况还会有很多,其实不在于 Compose, 而是协程的异常处理。后续如果遇到协程上游正常发生值之后,下游收集出现问题,要想到出现异常的情况。最佳方案是在下游需要对可能发生的异常进行及时处理。