Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unowned / Weak References with Closure

naru-jpn
January 31, 2017

Unowned / Weak References with Closure

変数の生存期間を playground を利用しながら確認していきます。

naru-jpn

January 31, 2017
Tweet

More Decks by naru-jpn

Other Decks in Programming

Transcript

  1. 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
  2. class Object { var child: Object? weak var weakChild: Object?

    var closure: (() -> ())? weak var currentExecutor: Executor? } ؍ଌ༻Ϋϥε Object
  3. 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? ม਺͕ղ์͞Ε͍ͯΔ͔Ͳ͏͔ͷ֬ೝ
  4. object object if true { let object: Object = Object()

    weakObject = object // 1 } // 2 1. ม਺͸είʔϓΛൈ͚ͨΒղ์͞ΕΔ
  5. object child object child if true { let object: Object

    = Object() object.child = object weakObject = object // 1 } // 2 weakObject?.child = nil 2. ࢀরʹΑΔϝϞϦϦʔΫ
  6. object child object child if true { let object: Object

    = Object() object.weakChild = object weakObject = object // 1 } // 2 weakObject?.child = nil 3. weak Λ࢖ͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
  7. object closure object closure if true { let object: Object

    = Object() object.closure = { print("\(object)") } weakObject = object // 1 } // 2 weakObject?.closure = nil 4. Ϋϩʔδϟ͔ΒͷࢀরʹΑΔϝϞϦϦʔΫ
  8. object closure object closure if true { let object: Object

    = Object() object.closure = { [weak object] in print("\(object)") } weakObject = object // 1 } // 2 5. [weak ---] Λ࢖ͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
  9. 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] Λ࢖͏৔߹
  10. object closure object object closure object είʔϓ಺ ؔ਺࣮ߦ࣌ ؔ਺ऴྃ࣌ είʔϓ֎

    Executor Executor if true { let object: Object = Object() object.printWeakSelf() weakObject = object } 6. Executor ͷΫϩʔδϟ಺Ͱ [weak self] Λ࢖͏৔߹
  11. 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] Λ࢖Θͳ͍৔߹
  12. object closure object object closure object Executor Executor είʔϓ಺ ؔ਺࣮ߦ࣌

    ؔ਺ऴྃ࣌ είʔϓ֎ if true { let object: Object = Object() object.printSelf() weakObject = object } 7. Executor ͷΫϩʔδϟ಺Ͱ [weak self] Λ࢖Θͳ͍৔߹
  13. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖͏৔߹
  14. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ object closure closure Executor object closure Executor

    if true { let object: Object = Object() object.printAsynchronousSelf() weakObject = object } sleep(UInt32(3.0)) 8. ඇಉظॲཧͰ [weak self] Λ࢖͏৔߹
  15. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖Θͳ͍৔߹
  16. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖Θͳ͍৔߹