Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
coroutinesで非同期ページネーション
Search
Keita Kagurazaka
June 29, 2017
Programming
1
660
coroutinesで非同期ページネーション
2017/06/29に開催された第6回Kotlin勉強会 @ Sansanの発表スライドです。
Keita Kagurazaka
June 29, 2017
Tweet
Share
More Decks by Keita Kagurazaka
See All by Keita Kagurazaka
SELECT FOR UPDATEの話
kkagurazaka
0
410
Mobileアプリのアーキテクチャ設計法
kkagurazaka
2
1.4k
原理から完全理解するDagger Hilt Migration
kkagurazaka
1
1.9k
今後のJetpackでAndroid開発はこう変わる!
kkagurazaka
16
6.2k
外部SDKのViewにマスク処理をする方法と罠
kkagurazaka
0
990
AWAのフルリニューアルを支えたアーキテクチャ
kkagurazaka
1
900
CQRS Architecture on Android
kkagurazaka
7
3k
suspending functionの裏側
kkagurazaka
3
450
async/awaitで快適非同期ライフ
kkagurazaka
4
1.9k
Other Decks in Programming
See All in Programming
私の後悔をAWS DMSで解決した話
hiramax
4
210
ぬるぬる動かせ! Riveでアニメーション実装🐾
kno3a87
1
230
MCPとデザインシステムに立脚したデザインと実装の融合
yukukotani
4
1.5k
testingを眺める
matumoto
1
140
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
350
The Past, Present, and Future of Enterprise Java with ASF in the Middle
ivargrimstad
0
170
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
2025 年のコーディングエージェントの現在地とエンジニアの仕事の変化について
azukiazusa1
24
12k
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
240
プロパティベーステストによるUIテスト: LLMによるプロパティ定義生成でエッジケースを捉える
tetta_pdnt
0
4.3k
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
個人軟體時代
ethanhuang13
0
330
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
113
20k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.8k
4 Signs Your Business is Dying
shpigford
184
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
How to Think Like a Performance Engineer
csswizardry
26
1.9k
Transcript
coroutinesで 非同期ページネーション 2017/06/29 第6回 Kotlin勉強会@Sansan Keita Kagurazaka
こんな実装、経験ありませんか?
1. APIやDBからLIMIT分だけデータを読み込み 2. 読み込んだデータをUIに一覧表示 3. 下までスクロールしていくと次のページを読み込み
いわゆる無限スクロール 1. APIやDBからLIMIT分だけデータを読み込み 2. 読み込んだデータをUIに一覧表示 3. 下までスクロールしていくと次のページを読み込み
無限スクロールの面倒Point • 現在のページ番号などの管理を呼び出し側が行わなければ ならない • 下までスクロールしたら次を呼ぶというボイラーテンプレートな ScrollListenerが必要 次のアイテムを要求したらいい感じに 読み込んで返してくれないものか
それ、coroutinesでできるよ
自己紹介 • アカウント ◦ Twitter: @kkagurazaka ◦ Github: k-kagurazaka •
Sansan Android developer • Kotlin Love! RxProperty for Android https://github.com/k-kagurazaka/rx-property-android AsyncPermissions https://github.com/k-kagurazaka/async-permissions
本題の前にcoroutinesの復習
coroutines (コルーチン) とは • 中断・再開可能な関数のようなもの ◦ Threadクラスのように作成して、実行する • 中断している間はスレッドをブロックしない ◦
non-blocking • 特定のスレッドに紐付かない ◦ 中断前と再開後で別スレッドで動かせる • 中断時・完了時に値を返せる
コルーチンの作成 ビルダー launch async<T> 戻り値 Job Deferred<T> 使い方 val job
= launch(CommonPool) { // 処理 } job.join() val deferred = async(CommonPool) { // T型を返す処理 } val result = deferred.await() 説明 値を返さないコルーチンを作成するビル ダー。 Jobをjoinすると完了まで中断、 cancelで キャンセルできる。 T型の値を返すコルーチンを作成するビル ダー。 Deferred<T>をawaitすると完了まで中断、 cancelでキャンセルできる。
中断点 = suspending function / lambda • suspending function ◦
suspend fun hoge() { } • suspending lambda ◦ val lambda: suspend () -> Unit = { } • suspending function / lambda は suspending function / lambda からしか呼べない
スレッドの切り替え suspend fun <T> run(context: CoroutineContext, block: suspend () ->
T): T • 使い方 suspend fun loadItems() { val items = run(CommonPool) { getItems() } run(UI) { itemList.addAll(items) } } suspend fun taskAonUI() = run(UI) { // 処理 }
コルーチンの中断と再開例 // コルーチン作成&開始 launch(CommonPool) { val hoge = ... taskAonUI()
// UIスレッドでtaskAを実行中はこのコルーチンは中断 val huga = … // taskAが完了するとここからコルーチンを再開 } 中断している間は、コルーチンはスレッドをブロックしない • taskAの実施中に別のコルーチンがスレッドを使える • taskA完了後もまだ使われてたら別のスレッドで再開する
複数の値を扱いたいときは?
Channel
Channelとは • ブロックの代わりに中断するBlockingQueue • suspend fun send(value: T) ◦ Channelのcapacityに空きがあれば送信
◦ なければ空くまで中断 • suspend fun receive(): T ◦ Channelに値があれば受信 ◦ なければ値が来るまで中断 capacityが0の場合はsendとreceiveが揃ったら送受信
コルーチン間でのデータ受け渡し val channel = Channel<Int>() // デフォルトではcapacity = 0のチャンネル launch(CommonPool)
{ repeat(5) { println(channel.receive()) } // 5回受信 } launch(CommonPool) { repeat(10) { channel.send(it) } // 0から4まで送信、5の送信時に中断 }
ようやく本題
Channelを使って ページネーションを実現する
suspend fun getBooks(page: Int, limit: Int): List<Book> // APIやらDBやらから取得
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ // 受信 (receive) 専用 Channel val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() // capacity = 0 の Channel 作成 launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { // スレッドプールで動くコルーチン起動 var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel // 作ったチャンネルを返す }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { // 無限ループで val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) // ページ数をincrementしながら取得 if (books.isEmpty()) break books.forEach { channel.send(it) } if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } // 取得したものを Channel に送信 if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break // 空か books.forEach { channel.send(it) } if (books.size < LIMIT) break // LIMIT より少なかったら無限ループから抜ける } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
{ val channel = Channel<Book>() launch(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { channel.send(it) } // 受信されないかぎりここで中断 if (books.size < LIMIT) break } channel.close() } return channel }
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
= produce(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { send(it) } if (books.size < LIMIT) break } } 簡単に書けるメソッドもあるよ!
suspend fun getBooks(page: Int, limit: Int): List<Book> fun loadBooks(): ReceiveChannel<Book>
= produce(CommonPool) { var page = 0 while (true) { val books = getBooks(page++, LIMIT) if (books.isEmpty()) break books.forEach { send(it) } if (books.size < LIMIT) break } } // 20個読み込む (LIMIT < 20でも問題なし) repeat(20) { val book = channel.receiveOrNull() ?: return@repeat }
ページネーションを意識せずに 必要な数だけ要求できる!
まとめ • coroutineは中断可能なThreadのようなもの • 中断中はスレッドをブロックしない • coroutine間で値をやりとりするときはChannelを使う • ページネーションを抽象化できたり、応用は様々 ◦
RxJavaのdebounceとかも書けるよ!
Thanks!