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
はてなブックマークAndroidアプリでのKotlin導入
Search
Takuji Nishibayashi
June 26, 2017
Technology
3
3.4k
はてなブックマークAndroidアプリでのKotlin導入
Takuji Nishibayashi
June 26, 2017
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
58
kotlinx.datetime 使ってみた
takuji31
0
220
HiltのCustom Componentについて
takuji31
0
140
java.timeをAndroidで使う
takuji31
0
53
KSPを使ってコード生成
takuji31
0
280
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
940
kotlinx.serialization
takuji31
0
530
kanmoba-returns-02.pdf
takuji31
0
170
AndroidXとKotlin Coroutines
takuji31
0
330
Other Decks in Technology
See All in Technology
使えそうで使われないCloudHSM
maikamibayashi
0
170
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
初心者に Vue.js を 教えるには
tsukuha
5
390
新卒1年目が向き合う生成AI事業の開発を加速させる技術選定 / ai-web-launcher
cyberagentdevelopers
PRO
7
1.5k
visionOSでの空間表現実装とImmersive Video表示について / ai-immersive-visionos
cyberagentdevelopers
PRO
1
110
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
27
12k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
290k
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
9
120k
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
640
スプリントゴールにチームの状態も設定する背景とその効果 / Team state in sprint goals why and impact
kakehashi
2
100
WINTICKETアプリで実現した高可用性と高速リリースを支えるエコシステム / winticket-eco-system
cyberagentdevelopers
PRO
1
190
Forget efficiency – Become more productive without the stress
ufried
0
150
Featured
See All Featured
Happy Clients
brianwarren
97
6.7k
Optimizing for Happiness
mojombo
376
69k
RailsConf 2023
tenderlove
29
880
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
Producing Creativity
orderedlist
PRO
341
39k
A designer walks into a library…
pauljervisheath
202
24k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Speed Design
sergeychernyshev
24
570
The Art of Programming - Codeland 2020
erikaheidi
51
13k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Transcript
ͯͳϒοΫϚʔΫ AndroidΞϓϦͰͷ Kotlinಋೖ id:takuji31 @Kan Java party 2017
ҙࣄ߲ • AndroidΞϓϦ։ൃͷલఏࣝͱͳΔΑ͏ͳ ෦ͷղઆ͠·ͤΜ • ͜ͷεϥΠυʹॻ͔Ε͍ͯΔKotlinͷίʔυ ༷όʔδϣϯ1.1.2࣌ͷͷͰ͢
ࣗݾհ • ྛ ࢤ (ʹ͠͠ ͨ͘͡) • (hatena|twitter|github) takuji31 •
גࣜձࣾͯͳ ΞϓϦέʔγϣϯΤϯδχΞ • ͯͳϒοΫϚʔΫΞϓϦνʔϜॴଐ
ࣗݾհ • Android։ൃ 20102݄ʙ • Kotlin 201412݄ʙ • iOSWebͷ։ൃᅂΉ
׆ಈ • Koreference • KotlinͰSharedPreferencesͷϞσϧΛ࡞Δ ϥΠϒϥϦʔ • Kansai.kt ӡӦ
ϓϥΠϕʔτ • Χϝϥপॅਓ • ϚΠϘϥʔ (AVG175) • ࠷ۙ͏͗͞Λࣂ͍࢝Ί·ͨ͠ • ΞΠίϯ͏ͪͷࢠͰ͢
ͱ͜ΖͰ
Έͳ͞ΜAndroidΞϓϦ ։ൃͯ͠·͔͢ʁ
Kotlinॻ͍ͯ·͔͢ʁ
ࠓAndroidΞϓϦ։ൃʹ Ͳ͏ͬͯKotlinΛಋೖͯ͠ ͍͔͘Λ͠·͢
AGENDA • Kotlinʹ͍ͭͯ • ͯͳϒοΫϚʔΫʹ͍ͭͯ • ͯͳϒοΫϚʔΫΞϓϦνʔϜͷߏ • Kotlinಋೖ·ͰͷಓͷΓ •
Kotlinͷಋೖͱͦͷޙ • KotlinԽͰى͖Δ • KotlinͷݴޠػೳΛ׆༻͢Δ
Kotlin ʹ͍ͭͯ
JetBrainsࣾ (IntelliJ IDEAͷ։ൃݩ) ͕ ։ൃ͢Δݴޠ
Kotlinͷಛ • Swift like ͳه๏ • Null safety • ܕਪ
• ϓϩύςΟʔ • ϥϜμࣜ
Kotlinͷಛ • ֦ுؔ / ֦ுϓϩύςΟʔ • data class / sealed
class • delegation • Coroutine (ex. async/await)
ଟͷϞμϯͳػೳΛ උ͍͑ͯΔ
Google I/O 2017Ͱ Android ΞϓϦͷެࣜ։ൃݴޠʹ
ͯͳϒοΫϚʔΫ ʹ͍ͭͯ
גࣜձࣾͯͳ͕ӡӦ͢Δ ιʔγϟϧϒοΫϚʔΫ αʔϏε
Web Android iOS ϒϥβʔ֦ு
WebScalaͰͷϦχϡʔΞ ϧ͕ਐߦத
AndroidΞϓϦઌ Ϧϊϕʔγϣϯ (େنϦϑΝΫλϦϯά) ͕ྃ㊗
http://developer.hatenastaff.com/entry/ 2017/05/26/192200
ͯͳϒοΫϚʔΫ ΞϓϦνʔϜ ʹ͍ͭͯ
ϝϯόʔߏ • σΟϨΫλʔ • ϓϥϯφʔ • σβΠφʔ • Android /
iOSΞϓϦ྆ํͷσβΠϯ୲
ϝϯόʔߏ • ΤϯδχΞ3໊ • Swiftίϛολʔ • όϦόϦͷAndroidΞϓϦΤϯδχΞ(iOSษڧத) • takuji31 •
AndroidΞϓϦͷϝΠϯ୲takuji31
ΤϯδχΞશ෦Δ
શ෦ • AndroidΞϓϦ • iOSΞϓϦ • API • اըΞΠσΟΞग़͠ɺνʔϜӡӦʹखΛग़ ͢
ࣾͰ࠷ΞΫςΟϒͳϞό ΠϧΞϓϦ։ൃνʔϜ
Kotlin ಋೖ·ͰͷಓͷΓ
ͯͳͰͷ৽ٕज़ (ݴޠϛυϧΣΞ) ಋೖ
ಋೖ·Ͱ • ΤϯδχΞ͕ىҊ • ٕज़ձͰಋೖͷੋඇ͕ٞ͞ΕΔ • CTO͕ঝೝ
Kotlinͷ߹ • I/O Keynote͕ऴΘͬͨޙͷேɺKotlinಋೖ ىҊ (5/18) • ٕज़ձ • ঝೝ
(5/19) • ͜͜·Ͱ͓Αͦ1
Kotlin+ͯϒͷ߹ • (ि໌͚ͯ) 22νʔϜͷD͕ෆࡏͩͬͨͷ Ͱ1ճٳΈ • 23ேʹDͱձͯ͠ಋೖܾఆ • ଈಋೖ
Kotlin ಋೖͱͦͷޙ
͍Α͍ΑKotlinಋೖ
खॱ • طଘͷΞϓϦέʔγϣϯΛͲ͏KotlinԽ͍ͯ͠ ͔͘ߟ͑Δ • ಋೖ • KotlinԽ
طଘͷΞϓϦέʔγϣϯΛͲ ͏KotlinԽ͍͔ͯ͘͠
Ξϓϩʔν • શ෦Ұؾʹม͢Δ • ৽نίʔυ͚ͩKotlinͰॻ͘ • ৽نίʔυΛKotlinͰॻ͖ͭͭɺؔ࿈͢Δ෦ Λม͢Δ
શ෦Ұؾʹஔ͖͑Δ
શ෦Ұؾʹஔ͖͑Δ • ·ͱΊͯKotlinϓϥάΠϯͷࣗಈมΛߦ͏ • ৽نίʔυͪΖΜKotlin
ϝϦοτ • શ෦ஔ͖ΘͬͯεοΩϦ͢Δ • Javaͱͷ૬ޓӡ༻Λߟ͑Δඞཁ͕ͳ͘ͳΔ • ؾ͍͍࣋ͪ
σϝϦοτ • diff͕ഁ໓͢Δ • Ұؾʹมࣗಈมͷखͷಧ͔ͳ͍ͱ͜Ζ ΛखಈͰमਖ਼∞ • ஔ͖Θͬͨͷจ๏͚ͩͰɺKotlinΒ͍͠ॻ ͖ํԿͰ͖͍ͯͳ͍
࿘ອͰ͢Ͷ
৽نίʔυ͚ͩKotlinͰॻ͘
৽نίʔυ͚ͩKotlinͰॻ͘ • ৽͍͠ϑΝΠϧ͚ͩKotlinʹ͢Δ • طଘͷJavaίʔυϊʔλον • طଘ෦ͷमਖ਼ͦͷ··JavaͰॻ͘
ϝϦοτ • มʹΑΔdiff͕ͳ͍ • KotlinͷֶशϖʔεΛίϯτϩʔϧ͍͢͠ • ͱΓ͔͋͑ͣΒͳ͔ͬͨΒJavaͰॻ͍ͨΒ ͍͍ͱ͍͏͜ͱʹͰ͖Δ • มθϩ
σϝϦοτ • ಋೖײബ͍ • Javaͱͷ૬ޓӡ༻Λҙࣝͨ͠ίʔυΛॻ͔ͳ͚ΕͳΒͳ͍ • JavaͱKotlinͰ಄ͷίϯςΩετεΠον͕ൃੜ͢ΔͷͰ͋·Γੜ ࢈ੑग़ͳ͍ʢݸਓͷײͰ͢ʣ • ݁ہJava͔ͬΓॻ͔ΕͨΓ͢Δ͔͠Εͳ͍
• Ϟμϯͳػೳ͕͍ͮΒ͔ͬͨΓ͢Δʢ֦ுؔͱ͔ʣ • ؾ࣋ͪΑ͘ͳ͍
҆શࡦͬͯ ײ͡Ͱ͢Ͷ
৽نίʔυΛKotlinͰॻ͖ͭ ͭɺؔ࿈͢Δ෦Λม͢Δ
৽نKotlinʴม • ৽نίʔυKotlinͰॻ͘ • ͦͷίʔυΛݺͼग़͢ઌɺͦ͜Ͱ͏ίʔ υΛKotlinʹม͢Δ • طଘͷΫϥεʹϝιουΛՃ͢ΔΑ͏ͳ࣌ ʹKotlinʹม͢Δ
ϝϦοτ • ൺֱతߴ͍ಋೖײ • ͻͨ͢ΒKotlinΛॻ͚Δ • Javaͱͷ૬ޓӡ༻Λ΄ͱΜͲҙࣝ͠ͳͯ͘Α ͘ͳΔ • ؾ͍͍࣋ͪ
σϝϦοτ • diff·͊·͍͊͢͜͝ͱʹͳΔ • ͋ͳͨͱKotlinࠓֶ͙͢शɺͱ͍͏ײ͡ʹͳΔ • มίετ͋Δ • Ͳ͜·Ͱม͢Δ͔ͷઢҾ͖͕͍͠
όϥϯε͕ ྑͦ͞͏Ͱ͢Ͷ
Ξϓϩʔν • શ෦Ұؾʹม͢Δ • ৽نίʔυ͚ͩKotlinͰॻ͘ • ৽نίʔυΛKotlinͰॻ͖ͭͭɺؔ࿈͢Δ෦ ΛKotlinʹม ࠾༻
ಋೖ
Gradleͷઃఆ ΛՃ
Tools Kotlin Configure Kotlin in Project
͋ͱ Android Studioʹͤͯ ͓͚ऴΘΔ
aptΛkaptʹ ஔ͖͑Δ
Kotlin͚ͷ ϥΠϒϥϦʔಋೖ
ϥΠϒϥϦʔΛಋೖ͢Δ͔ • KotlinͷݴޠػೳΛ࠷େݶ׆༻͢ΔͨΊʹඞ ཁ • KotlinΦϯϦʔͷϥΠϒϥϦʔΞΫςΟϒͳ ͷ͕গͳ͍ͷͰʁ • KotlinͷݴޠόʔδϣϯͷมߋʹϥΠϒϥϦʔ ͕͍͍͚ͭͯΔ͔ʁ
ಋೖͷࢦ • طଘϥΠϒϥϦʔͷ֦ுੵۃతʹಋೖͯ͠ ͍͘ • KotlinΦϯϦʔͷϥΠϒϥϦʔ৻ॏʹબͿ
KotlinԽʹͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1 • RxKotlin • RxJavaͷKotlin֦ு • ศརͳ֦ுؔ܈
KotlinԽʹͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1 • PaperParcel • JavaͷParcelableࣗಈੜϥΠϒϥϦʔ • ͜Ε·ͰIDEͷࣗಈੜͰͳΜͱ͔͍ͯͨ͠ • ͔͔ͤͬͩ͘Βboilerplate
codeݮΒ͍ͨ͠ͷͰಋೖ • Kotlinͷdata classαϙʔτ • ଞͷParcelableܥϥΠϒϥϦʔΑΓKotlinͱͷ૬ੑ͕Α͔ͬͨ
KotlinԽʹͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1 • Koreference • ࡞ • SharedPreferencesΛ͍͍ײ͡ʹModelͬΆ͘ѻ͏ϥΠϒϥϦʔ • ϓϩύςΟʔΛఆٛ͢Δ͚ͩͰSharedPreferencesͷΛૢ࡞Ͱ͖Δ
• RxJava2ʹΑΔมߋࢹαϙʔτ • ͱʹ͔͘खͰSharedPreferencesͷॲཧॻ͖ͨ͘ͳ͍ͷͰಋೖ • υοάϑʔσΟϯά݉Ͷͯ
ϥΠηϯεදه
KotlinApache2ϥΠηϯ ε
ඪ४ϥΠϒϥϦʔ͕ґଘͰ ೖΔͷͰϥΠηϯεදه ͠·͠ΐ͏
KotlinԽ
·ͣࢼͯ͠Έͨ
৽͍͠ActivityΛKotlinͰ
৽͍͠ActivityΛKotlinͰ • ৽نϑΝΠϧϝχϡʔͷKotlin ActivityΛબ • Ұ୴ଞͷΫϥεมͤͣʹActivity͚ͩ KotlinͰॻ͘ • Pull Request
-> merge
طଘͷRepositoryʹ ϝιουΛՃ͢Δ
طଘͷRepositoryʹϝιουΛ Ճ͢Δ • ΫϥεϑΝΠϧΛCode Convert Java file to Kotlin fileͰKotlinʹ
ม • KotlinΒ͘͠ॻ͚Δͱ͜Ζॻ͖͑Δ • ֦ுؔ • ϓϩύςΟʔ • ϥϜμࣜ • Repository͔Βฦ͢modelKotlinͷdata class
طଘͷRepositoryʹϝιουΛ Ճ͢Δ • ϩδοΫͷ࣮ • Pull Request merge
Kotlin ಋೖͰى͖Δ
diffഁ໓
ΫϥεAΛJava Kotlin ͢Δ = A.java Λফͯ͠ A.ktΛՃ
2ഒͷdiff
ίʔυͷमਖ਼ΛೖΕͨΒɺ ߋʹͦͷத͔ΒมߋΛݟ͚ͭ ͳ͚ΕͳΒͳ͍ɻ
ม͚ͩઌʹ͓ͬͯ͘ (Pull RequestΛ͚Δ)
gitྺ࢙ফ໓
ม͢Δ͜ͱͰผϑΝΠϧʹ ͳΔͷͰɺgitͷཤྺ͕͑ͳ ͘ͳΔ
ࣗಈม͕gitͰrenameѻ͍ ʹͳΕϥοΩʔ͕ͩɺͦ͏ ͍͏ϑΝΠϧن͕খ͍͞ ͷͰࠔΒͳ͍͜ͱ͕ଟ͍
࠷ѱͷղܾํ๏
.ktʹϦωʔϜͯ͠ίϛοτ .javaʹͯ͠มͯ͠ ίϛοτ
͜ͷํ๏Ͱ git log --follow Ͱ͑ΔΑ͏ʹͳΔ
(ͬͯͳ͍)
ϨϏϡʔίετ ߴ͗͢
ίʔυϨϏϡϫʔ͕Kotlinʹ ׳Εͯͳ͗ͯ͢͞ϨϏϡʔਫ਼ ͕Լ͕ͬͨΓɺεϐʔυ͕ མͪΔ
׳Εͷ
ϒοΫϚʔΫΞϓϦνʔϜ ͰϖΞͰίʔυϨϏϡʔ
ձͰٙΛղܾ
Kotlinॻ͚ͳ͍
͍͖ͳΓ৽͍͠ݴޠॻ͚ͳ͍ (ͨΓલ)
ॻ͚Δਓ͕νʔϜʹ1,2ਓ͔͠ ͍ͳ͍…ʁ
ಛʹJavaҎ֎ॻ͍ͨ͜ͱ͕ ͳ͍ɺͱ͍͏ਓ
≒ Ϟμϯͳݴޠͷॻ͖ํ͕ ͔Βͳ͍
ॻ͚Δਓͱ ϖΞϓϩɺϖΞϨϏϡʔ ͠·͠ΐ͏
ϒοΫϚʔΫΞϓϦνʔϜ ͳΜͱͳ͘ॻ͚Δਓ͕ ἧ͍ͬͯͨ☺
୭ॻ͚Δਓ͕͍ͳ͍ͳΒ ·ͣಋೖલʹֶशͯ͘͠Εʂ
kotlinlang.org/docs/ reference/
try.kotlinlang.org
Kotlin ͷݴޠػೳΛ ׆༻͢Δ
Property
Πϯελϯεͷঢ়ଶΛ ද͢Έ
getter (+setter) (+ backing field)
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } }
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } } ಡΈऔΓઐ༻
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } } ಡΈऔΓઐ༻ ಡΈॻ͖Մ
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } } ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } } ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ
Property class User(id: Long, name: String) { val id: Long
var name: String var registered: Boolean = false init { this.id = id this.name = name } } ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ JOJUϒϩοΫͰ ॳظԽ
͜͏ஔ͖͑ΒΕ·͢ class User(val id: Long, var name: String) { var
registered: Boolean = false }
͜͏ஔ͖͑ΒΕ·͢ class User(val id: Long, var name: String) { var
registered: Boolean = false } ίϯετϥΫλʔͰ ϓϩύςΟʔఆٛ͢Δͱɺࣗಈతʹೖ͞ΕΔ
registeredΛड͚औΕΔΑ͏ʹ class User( val id: Long, var name: String, var
registered: Boolean = false )
registeredΛड͚औΕΔΑ͏ʹ class User( val id: Long, var name: String, var
registered: Boolean = false ) SFHJTUFSFEΛ͞ͳ͔ͬͨΒGBMTF͕ೖ͞ΕΔ
data class
ԿΒ͔ͷσʔλʔΛอ࣋͢Δ ͨΊͷΫϥε
ModelDTOΛ࡞ΔͨΊͷ ศརػೳຬࡌ
ex.) طଘͷmodelΛKotlinʹ ม͍ͨ͠
มConvert Java file to Kotlin fileͰ
୯७ͳม͚ͩͰΑ͍ͷ͔ʁ
୯७ͳม͚ͩͰΑ͍ͷ͔ʁ • Immutableʹ͍ͨ͠ • equals/hashCode/toStringͷ࣮
Immutableʹ
શͯͷϓϩύςΟʔΛvalͰ ఆٛ + constructorͰೖ
એݴ class User( val id: Long, val name: String, val
registered: Boolean )
͏ fun main(args: Array<String>) { val unregisteredUser = User( id
= 114514L, name = "takuji31", registered = false ) val user = register(unregisteredUser) print(user) } fun register(user: User): User { // do something return User(user.id, user.name, true) }
ίϐʔΛͬͱ͍͍ײ͡ʹ͠ ͍ͨ
data classΛ͏
એݴ data class User( val id: Long, val name: String,
val registered: Boolean = false )
એݴ data class User( val id: Long, val name: String,
val registered: Boolean = false ) ಄ʹEBUB Λ͚ͭΔ
એݴ data class User( val id: Long, val name: String,
val registered: Boolean = false ) ಄ʹEBUB Λ͚ͭΔ શͯͷϓϩύςΟʔΛ DPOTUSVDUPSͰఆٛ
͏ fun register(user: User): User { // do something return
user.copy(registered = true) }
͏ fun register(user: User): User { // do something return
user.copy(registered = true) } DPQZϝιουͷҾʹ มߋ͍͚ͨͩ͠Λ͢ͱ ίϐʔ͞ΕΔ
equals/hashCode/toString ͷ࣮
data classΛ͏ͱࣗಈੜ ͞ΕΔ
ͯͳϒοΫϚʔΫAndroid ΞϓϦͰ modelΛdata class ੵۃతʹஔ͖͍͑ͯ·͢
Extension
֦ுؔ/֦ுϓϩύςΟʔ
طଘͷΫϥεʹ ؔϓϩύςΟʔΛੜ͢ (Α͏ʹݟͤΔ)Έ
ϑϨʔϜϫʔΫϥΠϒϥϦʔ ͷΫϥεΛ֦ு͍ͯ͘͠ͱΑ ͍
FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity? get() = activity as BaseActivity?
val Fragment.actionBar: ActionBar? get() = baseActivity?.supportActionBar
FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity? get() = activity as BaseActivity?
val Fragment.actionBar: ActionBar? get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘
FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity? get() = activity as BaseActivity?
val Fragment.actionBar: ActionBar? get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ௨ৗͷ ϓϩύςΟʔͱಉ͡
RealmͷProviderΛ֦ுؔͰ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> { val realm = get()
realm.use { return Observable.create { emitter -> val instance = get() emitter.setCancellable { instance.close() } emitter.onNext(instance) } } }
RealmͷProviderΛ֦ுؔͰ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> { val realm = get()
realm.use { return Observable.create { emitter -> val instance = get() emitter.setCancellable { instance.close() } emitter.onNext(instance) } } } ֦ு͍ͨ͠ܕ Λॻ͘
RealmͷProviderΛ֦ுؔͰ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> { val realm = get()
realm.use { return Observable.create { emitter -> val instance = get() emitter.setCancellable { instance.close() } emitter.onNext(instance) } } } ֦ு͍ͨ͠ܕ Λॻ͘ (FOFSJDTͷ ܕύϥϝʔλʔࢦఆՄ ϓϩύςΟʔಉ༷
RealmͷProviderΛ֦ுؔͰ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> { val realm = get()
realm.use { return Observable.create { emitter -> val instance = get() emitter.setCancellable { instance.close() } emitter.onNext(instance) } } } ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ௨ৗͷ ؔͱಉ͡ (FOFSJDTͷ ܕύϥϝʔλʔࢦఆՄ ϓϩύςΟʔಉ༷
ҙ
࣮ࡍʹ֦ு͞ΕΔ ͷͰͳ͘ɺ ࣮ମstaticͳ ϝιουݺͼग़͠
= ֦ுϓϩύςΟʔϑΟʔ ϧυΛ࣋ͯͳ͍
ఘΊͯଞͷΞϓϩʔνΛ ߟ͑·͠ΐ͏
֦ு͗͢͠Δͱ Θ͚͕Θ͔Βͳ͘ͳΔ
Γա͗ҙ
Delegated property
ҕৡϓϩύςΟʔ
ॲཧΛҕৡ͢Δ ϓϩύςΟʔΛ ࡞Δ͜ͱ͕Ͱ͖Δ
σϑΥϧτͷ Delegated property(Ұ෦) • Delegates.notNull • not-null͕ͩॳظԽ࣌ʹॳظԽͰ͖ͳ͍ϓϩύςΟʔ • nullͷ··ΞΫηεͨ͠Βྫ֎ •
lazy • ԆॳظԽ • ࠷ॳͷΞΫηε࣌ʹҾͰͨ͠ϒϩοΫͷΓΛฦͯ͠ Ωϟογϡ͢Δ
ଟ͕࣌ؒΓͳ͍ͷͰ
see. github.com/takuji31/ Koreference
Enjoy Kotlin Life