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
64
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
59
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kotlinx.serialization
takuji31
0
540
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
330
Other Decks in Technology
See All in Technology
安心してください、日本語使えますよ―Ubuntu日本語Remix提供休止に寄せて― 2024-11-17
nobutomurata
1
990
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
120
強いチームと開発生産性
onk
PRO
33
11k
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
130
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
[FOSS4G 2024 Japan LT] LLMを使ってGISデータ解析を自動化したい!
nssv
1
210
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
0
110
Featured
See All Featured
Designing Experiences People Love
moore
138
23k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Faster Mobile Websites
deanohume
305
30k
BBQ
matthewcrist
85
9.3k
Being A Developer After 40
akosma
86
590k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
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