Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
rememberUpdatedState の使いどころを考える
Search
akkiee76
November 12, 2023
Technology
0
630
rememberUpdatedState の使いどころを考える
「potatotips #85 iOS/Android開発Tips共有会」で発表したスライドになります。
https://potatotips.connpass.com/event/299247/
akkiee76
November 12, 2023
Tweet
Share
More Decks by akkiee76
See All by akkiee76
Graph Art with Charts API – Beyond Data Visualization
akkie76
0
180
Meet the Translation API
akkie76
0
430
コードレビューで開発を加速させるAIコードレビュー
akkie76
1
680
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
6k
コードレビューを支援するAI技術の応用
akkie76
5
1.2k
オブジェクト指向コードレビューの新しいアプローチ
akkie76
3
9.5k
Jetpack Compose で Adaptive Layout に対応しよう
akkie76
0
1.1k
Observationではじめる値監視
akkie76
4
4.8k
TextField 表示スタイル変更の 有効活用例 5 選
akkie76
0
740
Other Decks in Technology
See All in Technology
[デモです] NotebookLM で作ったスライドの例
kongmingstrap
0
130
re:Invent 2025 ふりかえり 生成AI版
takaakikakei
1
190
学習データって増やせばいいんですか?
ftakahashi
2
300
Debugging Edge AI on Zephyr and Lessons Learned
iotengineer22
0
170
計算機科学をRubyと歩む 〜DFA型正規表現エンジンをつくる~
ydah
3
230
Kiro Autonomous AgentとKiro Powers の紹介 / kiro-autonomous-agent-and-powers
tomoki10
0
390
寫了幾年 Code,然後呢?軟體工程師必須重新認識的 DevOps
cheng_wei_chen
1
1.3k
「Managed Instances」と「durable functions」で広がるAWS Lambdaのユースケース
lamaglama39
0
300
非CUDAの悲哀 〜Claude Code と挑んだ image to 3D “Hunyuan3D”を EVO-X2(Ryzen AI Max+395)で動作させるチャレンジ〜
hawkymisc
1
170
日本Rubyの会の構造と実行とあと何か / hokurikurk01
takahashim
4
1k
AI活用によるPRレビュー改善の歩み ― 社内全体に広がる学びと実践
lycorptech_jp
PRO
1
200
会社紹介資料 / Sansan Company Profile
sansan33
PRO
11
390k
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Designing for Performance
lara
610
69k
Code Reviewing Like a Champion
maltzj
527
40k
The Language of Interfaces
destraynor
162
25k
Building an army of robots
kneath
306
46k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
Making Projects Easy
brettharned
120
6.5k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Embracing the Ebb and Flow
colly
88
4.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
A Tale of Four Properties
chriscoyier
162
23k
Bash Introduction
62gerente
615
210k
Transcript
©2023 RAKUS Co., Ltd. rememberUpdatedState の使いどころを考える potatotips #85 iOS/Android開発Tips共有会 2023/11/14
@akkiee76
自己紹介 • Akihiko Sato • Rakus Inc. • 楽楽精算 •
@akkiee76 / X
rememberUpdatedState とは?
Compose における Side effect APIs の1つ
Compose 可能な関数の範囲外で発生するアプリの状態の変化 • 予想しない再コンポジション • 異なる順序でのコンポーザブルの再コンポジション • 破棄可能な再コンポジション Compose における
Side effect(副作用)とは? コンポーザブルは Side effect がないようにするのが理想的 https://developer.android.com/jetpack/compose/side-effects
• スナックバーを表示する • 1 回限りのイベントをトリガーする • 特定の状態で別の画面に遷移する Side effect が必要なケース
https://developer.android.com/jetpack/compose/side-effects 予測可能な方法で Side effect を実行する必要がある
rememberUpdatedState API とは? 値が変化しても再起動すべきでない作用を 参照することができる Side effect API https://developer.android.com/jetpack/compose/side-effects?hl=jp#rememberupdatedstate 再起動が高コストである作用にアプローチができる
最新の時刻を Snackbar に表示するユースケース 1. remember を利用した場合 2. LaunchedEffect のキーを更新した場合 3.
rememberUpdatedState API を利用した場合
1. remember を利用した場合
var currentTime by remember { mutableStateOf(Date().time.toString()) } Button( onClick =
{ currentTime = Date().time.toString() } ) { Text(currentTime) } SnackbarScreen ( currentTime = currentTime, snackbarHostState = snackbarHostState ) 時刻を保持 時刻を更新 時刻を渡す
@Composable fun SnackbarScreen( currentTime: String, snackbarHostState: SnackbarHostState ) { LaunchedEffect(Unit)
{ delay(3000) snackbarHostState.showSnackbar(message = currentTime) } } 一度だけ実行
2. LaunchedEffect の key を更新した場合
@Composable fun SnackbarScreen( currentTime: String, snackbarHostState: SnackbarHostState ) { LaunchedEffect(currentTime)
{ delay(3000) snackbarHostState.showSnackbar(message = currentTime) } } 何も表示されない LaunchedEffect が再起動し Coroutine がキャンセル currentTimeが更新され続ける
3. rememberUpdatedState API を利用した場合
@Composable fun SnackbarScreen ( currentTime: String, snackbarHostState : SnackbarHostState )
{ val rememberTime by rememberUpdatedState (newValue = currentTime) LaunchedEffect (Unit) { delay(3000) snackbarHostState .showSnackbar( message = rememberTime) } } 最新の値が表示 値が変化しても LaunchedEffect の Coroutine はキャンセルされない currentTimeが更新され続ける
@Composable fun <T> rememberUpdatedState(newValue: T): State<T> = remember { mutableStateOf(newValue)
}.apply { value = newValue } オブジェクトをキャプチャーし、中身を更新している
まとめ rememberUpdatedState API の使いどころ • Coroutine の処理をキャンセルさせたくないケース ◦ 重い通信処理(ファイルダウンロード)など •
コールバックを利用するケース ◦ エラーハンドリングなど • 異なる Composable などで変更される値を参照するケース
Thank you 🎉