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
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
73
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kotlinx.serialization
takuji31
0
550
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
340
Other Decks in Technology
See All in Technology
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
180
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
TSKaigi 2024 の登壇から広がったコミュニティ活動について
tsukuha
0
160
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
210
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1k
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
非機能品質を作り込むための実践アーキテクチャ
knih
3
870
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
380
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Thoughts on Productivity
jonyablonski
67
4.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Building Applications with DynamoDB
mza
91
6.1k
Agile that works and the tools we love
rasmusluckow
328
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Invisible Side of Design
smashingmag
298
50k
Navigating Team Friction
lara
183
15k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Typedesign – Prime Four
hannesfritz
40
2.4k
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