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
Unowned / Weak References with Closure
Search
naru-jpn
January 31, 2017
Programming
1
410
Unowned / Weak References with Closure
変数の生存期間を playground を利用しながら確認していきます。
naru-jpn
January 31, 2017
Tweet
Share
More Decks by naru-jpn
See All by naru-jpn
配信アプリのためのリアルタイムプッシュ通知ぼかしの夢
narujpn
3
800
PiPを応用した配信コメントバー機能の開発秘話と技術の詳解 / pip_streaming_comment_bar
narujpn
3
3.7k
Updating an App to Use Swift Concurrency 解説
narujpn
2
260
PiP で実現するミラティブの配信コメントバー / pip-streaming-comment-bar
narujpn
0
1k
App Extension のスタックトレース情報からクラッシュを解析/集計する / Analyzing app extension's stack trace
narujpn
3
1.3k
ミラティブとWebRTC - WebRTC framework の中身を覗いてみよう / WebRTC framework AudioUnit Processing
narujpn
1
2.1k
CoreML3のオンデバイストレーニングでつくる母音推定
narujpn
0
390
AltConfと周辺の歩き方
narujpn
0
1.9k
エンジニア経験を活かしたスクラムマスターとして 開発チームとプロダクトを成長させる
narujpn
1
380
Other Decks in Programming
See All in Programming
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
快速入門可觀測性
blueswen
0
330
Haze - Real time background blurring
chrisbanes
1
510
Zoneless Testing
rainerhahnekamp
0
120
선언형 UI에서의 상태관리
l2hyunwoo
0
140
Beyond ORM
77web
2
210
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
Featured
See All Featured
How GitHub (no longer) Works
holman
311
140k
Optimising Largest Contentful Paint
csswizardry
33
3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Navigating Team Friction
lara
183
15k
It's Worth the Effort
3n
183
28k
GraphQLとの向き合い方2022年版
quramy
44
13k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Side Projects
sachag
452
42k
Embracing the Ebb and Flow
colly
84
4.5k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Transcript
Naruki Chigira - Timers inc. GitHub: naru-jpn, Twitter: @naruchigi Unowned
/ Weak References with Closure
มͷੜଘظؒɺؾʹͯ͠·͔͢ʁ
ΫϩʔδϟͰอ࣋͞ΕΔมɺ͑·͔͢ʁ
DispatchQueue.main.async { [weak self] in // ... }
ΫϩʔδϟͰʰ ͱΓ͋͑ͣ [weak self] ʱͯ͠·ͤΜ͔ʁ
Playground ͰڍಈΛࡉ͔͘ݟͯΈΑ͏ https://gist.github.com/naru-jpn/fa4c39ce2eda8a803358dad75d04058d
class Executor { // MARK: Exeute stored procedure let procedure:
() -> () init(procedure: @escaping () -> ()) { self.procedure = procedure } func execute() { self.procedure() } // MARK: Static execution static func execute(procedure: () -> ()) { procedure() } } ΫϩʔδϟΛ࣮ߦ͢ΔΫϥε Executor
class Object { var child: Object? weak var weakChild: Object?
var closure: (() -> ())? weak var currentExecutor: Executor? } ؍ଌ༻Ϋϥε Object
weak var weakObject: Object? = nil // スコープ内外での挙動を見るために if 文でネストさせる
// object が解放されていれば weakObject の中身は nil になる if true { let object: Object = Object() weakObject = object print(“\(weakObject)") // nil or not nil? } print(“\(weakObject)") // nil or not nil? ม͕ղ์͞Ε͍ͯΔ͔Ͳ͏͔ͷ֬ೝ
object object if true { let object: Object = Object()
weakObject = object // 1 } // 2 1. มείʔϓΛൈ͚ͨΒղ์͞ΕΔ
object child object child if true { let object: Object
= Object() object.child = object weakObject = object // 1 } // 2 weakObject?.child = nil 2. ࢀরʹΑΔϝϞϦϦʔΫ
object child object child if true { let object: Object
= Object() object.weakChild = object weakObject = object // 1 } // 2 weakObject?.child = nil 3. weak ΛͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
॥ࢀরવආ͚·͠ΐ͏
object closure object closure if true { let object: Object
= Object() object.closure = { print("\(object)") } weakObject = object // 1 } // 2 weakObject?.closure = nil 4. Ϋϩʔδϟ͔ΒͷࢀরʹΑΔϝϞϦϦʔΫ
object closure object closure if true { let object: Object
= Object() object.closure = { [weak object] in print("\(object)") } weakObject = object // 1 } // 2 5. [weak ---] ΛͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
Ϋϩʔδϟͷ॥ࢀরʹҙ
object closure object object closure object είʔϓ ࣮ؔߦ࣌ ؔऴྃ࣌ είʔϓ֎
Executor Executor extension Object { func printWeakSelf() { let executor: Executor = Executor(procedure: { [weak self] in debugPrint("\(self)") }) executor.execute() } } 6. Executor ͷΫϩʔδϟͰ [weak self] Λ͏߹
object closure object object closure object είʔϓ ࣮ؔߦ࣌ ؔऴྃ࣌ είʔϓ֎
Executor Executor if true { let object: Object = Object() object.printWeakSelf() weakObject = object } 6. Executor ͷΫϩʔδϟͰ [weak self] Λ͏߹
object closure object object closure object Executor Executor είʔϓ ࣮ؔߦ࣌
ؔऴྃ࣌ είʔϓ֎ extension Object { func printSelf() { let executor: Executor = Executor(procedure: { print("\(self)") }) executor.execute() } } 7. Executor ͷΫϩʔδϟͰ [weak self] ΛΘͳ͍߹
object closure object object closure object Executor Executor είʔϓ ࣮ؔߦ࣌
ؔऴྃ࣌ είʔϓ֎ if true { let object: Object = Object() object.printSelf() weakObject = object } 7. Executor ͷΫϩʔδϟͰ [weak self] ΛΘͳ͍߹
ΫϩʔδϟͰ self Λࢀরͯ͠ϦʔΫ͠ͳ͍߹͋Δ
ඇಉظॲཧػ࣌ ඇಉظॲཧऴྃ࣌ ࣮ؔߦ࣌ object closure closure Executor object closure Executor
extension Object { func printAsynchronousWeakSelf() { let executor: Executor = Executor(procedure: { let time: DispatchTime = .now() + 1.0 DispatchQueue.global().asyncAfter(deadline: time, execute: { [weak self] in print("\(self)") }) }) executor.execute() self.currentExecutor = executor } } 8. ඇಉظॲཧͰ [weak self] Λ͏߹
ඇಉظॲཧػ࣌ ඇಉظॲཧऴྃ࣌ ࣮ؔߦ࣌ object closure closure Executor object closure Executor
if true { let object: Object = Object() object.printAsynchronousSelf() weakObject = object } sleep(UInt32(3.0)) 8. ඇಉظॲཧͰ [weak self] Λ͏߹
ඇಉظॲཧػ࣌ ඇಉظॲཧऴྃ࣌ ࣮ؔߦ࣌ object closure object closure Executor object closure
Executor extension Object { func printAsynchronousSelf() { let executor: Executor = Executor(procedure: { DispatchQueue.global().asyncAfter(deadline: .now() + 1.0, execute: { print("\(self)") }) }) executor.execute() self.currentExecutor = executor } } 9. ඇಉظॲཧͰ [weak self] ΛΘͳ͍߹
ඇಉظॲཧػ࣌ ඇಉظॲཧऴྃ࣌ ࣮ؔߦ࣌ object closure object closure Executor object closure
Executor if true { let object: Object = Object() object.printAsynchronousSelf() weakObject = object } sleep(UInt32(3.0)) 9. ඇಉظॲཧͰ [weak self] ΛΘͳ͍߹
ඇಉظॲཧͰίʔϧόοΫ࣌ʹ object ͕ੜଘ͍ͯ͠Δ͔Ͳ͏͔͕มΘͬͯ͘Δ
ͨͩ͠ɺϦʔΫ͠ͳ͍͜ͱʹมΘΓͳ͍
తʹԠ͍͚ͯ͡ΒΕΔΑ͏ʹͳΓ͍ͨͰ͢Ͷ
͍͔ͭ [weak self] Λݟͨͱ͖ʹࢥ͍ΛͤͯΈ͍ͯͩ͘͞
None
None
None
None
None
Motivation / Concept https://developers.google.com/protocol-buffers/ Template message