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
400
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
760
PiPを応用した配信コメントバー機能の開発秘話と技術の詳解 / pip_streaming_comment_bar
narujpn
3
3.6k
Updating an App to Use Swift Concurrency 解説
narujpn
2
250
PiP で実現するミラティブの配信コメントバー / pip-streaming-comment-bar
narujpn
0
980
App Extension のスタックトレース情報からクラッシュを解析/集計する / Analyzing app extension's stack trace
narujpn
3
1.3k
ミラティブとWebRTC - WebRTC framework の中身を覗いてみよう / WebRTC framework AudioUnit Processing
narujpn
1
2k
CoreML3のオンデバイストレーニングでつくる母音推定
narujpn
0
390
AltConfと周辺の歩き方
narujpn
0
1.9k
エンジニア経験を活かしたスクラムマスターとして 開発チームとプロダクトを成長させる
narujpn
1
370
Other Decks in Programming
See All in Programming
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
140
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
ヤプリ新卒SREの オンボーディング
masaki12
0
130
みんなでプロポーザルを書いてみた
yuriko1211
0
260
CSC509 Lecture 12
javiergs
PRO
0
160
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
140
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Rails Girls Zürich Keynote
gr2m
94
13k
Optimizing for Happiness
mojombo
376
70k
Faster Mobile Websites
deanohume
305
30k
The Language of Interfaces
destraynor
154
24k
Automating Front-end Workflow
addyosmani
1366
200k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
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