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
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Realtime API 入門
riofujimon
0
150
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
CSC509 Lecture 11
javiergs
PRO
0
180
Featured
See All Featured
Music & Morning Musume
bryan
46
6.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Agile that works and the tools we love
rasmusluckow
327
21k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Writing Fast Ruby
sferik
627
61k
Speed Design
sergeychernyshev
24
610
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
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