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
66
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
60
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
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
いざ、BSC討伐の旅
nikinusu
2
780
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.7k
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
630
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
180
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
230
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
Lexical Analysis
shigashiyama
1
150
AI前提のサービス運用ってなんだろう?
ryuichi1208
8
1.4k
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Rails Girls Zürich Keynote
gr2m
94
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Designing for humans not robots
tammielis
250
25k
What's in a price? How to price your products and services
michaelherold
243
12k
RailsConf 2023
tenderlove
29
900
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
The Language of Interfaces
destraynor
154
24k
Happy Clients
brianwarren
98
6.7k
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