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
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
3
380
return文におけるstd::moveについて
onihusube
1
1.1k
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Jakarta EE meets AI
ivargrimstad
0
240
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
MCP with Cloudflare Workers
yusukebe
2
220
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
290
php-conference-japan-2024
tasuku43
0
270
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Facilitating Awesome Meetings
lara
50
6.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Bash Introduction
62gerente
608
210k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
A better future with KSS
kneath
238
17k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Git: the NoSQL Database
bkeepers
PRO
427
64k
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