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
74
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
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
3
2.4k
20241220_S3 tablesの使い方を検証してみた
handy
4
610
ハイテク休憩
sat
PRO
2
160
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
17
4.6k
大幅アップデートされたRagas v0.2をキャッチアップ
os1ma
2
540
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
150
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
200
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
530
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
2
390
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
200
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Building Applications with DynamoDB
mza
91
6.1k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
We Have a Design System, Now What?
morganepeng
51
7.3k
Side Projects
sachag
452
42k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Code Review Best Practice
trishagee
65
17k
Typedesign – Prime Four
hannesfritz
40
2.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Producing Creativity
orderedlist
PRO
341
39k
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