Unowned / Weak References with Closure

2bfb16e53b154afc9f141f831b4aaaf2?s=47 naru-jpn
January 31, 2017

Unowned / Weak References with Closure

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

2bfb16e53b154afc9f141f831b4aaaf2?s=128

naru-jpn

January 31, 2017
Tweet

Transcript

  1. Naruki Chigira - Timers inc. GitHub: naru-jpn, Twitter: @naruchigi Unowned

    / Weak References with Closure
  2. ม਺ͷੜଘظؒɺؾʹͯ͠·͔͢ʁ

  3. Ϋϩʔδϟ಺Ͱอ࣋͞ΕΔม਺ɺ௥͑·͔͢ʁ

  4. DispatchQueue.main.async { [weak self] in // ... }

  5. Ϋϩʔδϟ಺Ͱʰ ͱΓ͋͑ͣ [weak self] ʱͯ͠·ͤΜ͔ʁ

  6. Playground ͰڍಈΛࡉ͔͘ݟͯΈΑ͏ https://gist.github.com/naru-jpn/fa4c39ce2eda8a803358dad75d04058d

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

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

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

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

    = Object() object.weakChild = object weakObject = object // 1 } // 2 weakObject?.child = nil 3. weak Λ࢖ͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
  13. ॥؀ࢀর͸౰વආ͚·͠ΐ͏

  14. object closure object closure if true { let object: Object

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

    = Object() object.closure = { [weak object] in print("\(object)") } weakObject = object // 1 } // 2 5. [weak ---] Λ࢖ͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε
  16. Ϋϩʔδϟͷ॥؀ࢀরʹ΋஫ҙ

  17. 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] Λ࢖͏৔߹
  18. object closure object object closure object είʔϓ಺ ؔ਺࣮ߦ࣌ ؔ਺ऴྃ࣌ είʔϓ֎

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

    ؔ਺ऴྃ࣌ είʔϓ֎ if true { let object: Object = Object() object.printSelf() weakObject = object } 7. Executor ͷΫϩʔδϟ಺Ͱ [weak self] Λ࢖Θͳ͍৔߹
  21. Ϋϩʔδϟ಺Ͱ self Λࢀরͯ͠΋ϦʔΫ͠ͳ͍৔߹΋͋Δ

  22. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖͏৔߹
  23. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ object closure closure Executor object closure Executor

    if true { let object: Object = Object() object.printAsynchronousSelf() weakObject = object } sleep(UInt32(3.0)) 8. ඇಉظॲཧͰ [weak self] Λ࢖͏৔߹
  24. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖Θͳ͍৔߹
  25. ඇಉظॲཧ଴ػ࣌ ඇಉظॲཧऴྃ࣌ ؔ਺࣮ߦ࣌ 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] Λ࢖Θͳ͍৔߹
  26. ඇಉظॲཧͰ͸ίʔϧόοΫ࣌ʹ object ͕ੜଘ͍ͯ͠Δ͔Ͳ͏͔͕มΘͬͯ͘Δ

  27. ͨͩ͠ɺϦʔΫ͠ͳ͍͜ͱʹมΘΓ͸ͳ͍

  28. ໨తʹԠͯ͡࢖͍෼͚ΒΕΔΑ͏ʹͳΓ͍ͨͰ͢Ͷ

  29. ͍͔ͭ [weak self] Λݟͨͱ͖ʹࢥ͍Λ஘ͤͯΈ͍ͯͩ͘͞

  30. None
  31. None
  32. None
  33. None
  34. None
  35. Motivation / Concept https://developers.google.com/protocol-buffers/ Template message