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
710
PiPを応用した配信コメントバー機能の開発秘話と技術の詳解 / pip_streaming_comment_bar
narujpn
3
3.4k
Updating an App to Use Swift Concurrency 解説
narujpn
2
230
PiP で実現するミラティブの配信コメントバー / pip-streaming-comment-bar
narujpn
0
930
App Extension のスタックトレース情報からクラッシュを解析/集計する / Analyzing app extension's stack trace
narujpn
3
1.2k
ミラティブとWebRTC - WebRTC framework の中身を覗いてみよう / WebRTC framework AudioUnit Processing
narujpn
1
2k
CoreML3のオンデバイストレーニングでつくる母音推定
narujpn
0
380
AltConfと周辺の歩き方
narujpn
0
1.9k
エンジニア経験を活かしたスクラムマスターとして 開発チームとプロダクトを成長させる
narujpn
1
360
Other Decks in Programming
See All in Programming
今インフラ技術をイチから学び直すなら
yuhta28
1
100
LangChainの現在とv0.3にむけて
os1ma
3
730
快適な開発と高セキュリティを実現するCryptoKitを活用したCoreDataのデータ暗号化術
grandbig
1
310
LangChainでWebサイトの内容取得やGitHubソースコード取得
shukob
0
130
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
150
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
200
フロントエンドカンファレンス北海道2024 『小規模サイトでも使えるVite 〜HTMLコーディングをよりスマートに〜』長谷川広武(ハム)
h2ham
1
2.5k
Modular Monolith Go Server with GraphQL Federation + gRPC
110y
1
560
Scala におけるコンパイラエラーとの付き合い方
chencmd
1
200
マイグレーションコード自作して File-Based Routing に自動移行!! ~250 ページの歴史的経緯を添えて~
cut0
1
250
ECMAScript仕様を読むのに必要な知識 - ダイジェスト版
syumai
4
2.5k
全力の跳躍を捉える計測アプリを作る
ogijun2018
1
1.2k
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
The Power of CSS Pseudo Elements
geoffreycrofte
71
5.2k
BBQ
matthewcrist
83
9.1k
Web development in the modern age
philhawksworth
204
10k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
22
3.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
88
16k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
109
6.9k
Making Projects Easy
brettharned
113
5.8k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
165
48k
The World Runs on Bad Software
bkeepers
PRO
64
11k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
354
29k
Faster Mobile Websites
deanohume
304
30k
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