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. Naruki Chigira - Timers inc.
    GitHub: naru-jpn, Twitter: @naruchigi
    Unowned / Weak References with Closure

    View full-size slide

  2. ม਺ͷੜଘظؒɺؾʹͯ͠·͔͢ʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  8. class Object {
    var child: Object?
    weak var weakChild: Object?
    var closure: (() -> ())?
    weak var currentExecutor: Executor?
    }
    ؍ଌ༻Ϋϥε Object

    View full-size slide

  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?
    ม਺͕ղ์͞Ε͍ͯΔ͔Ͳ͏͔ͷ֬ೝ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. ॥؀ࢀর͸౰વආ͚·͠ΐ͏

    View full-size slide

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

    View full-size slide

  15. object
    closure
    object
    closure
    if true {
    let object: Object = Object()
    object.closure = { [weak object] in
    print("\(object)")
    }
    weakObject = object // 1
    }
    // 2
    5. [weak ---] Λ࢖ͬͯϝϞϦϦʔΫ͠ͳ͍Α͏ʹͨ͠έʔε

    View full-size slide

  16. Ϋϩʔδϟͷ॥؀ࢀরʹ΋஫ҙ

    View full-size slide

  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] Λ࢖͏৔߹

    View full-size slide

  18. object
    closure
    object object
    closure
    object
    είʔϓ಺ ؔ਺࣮ߦ࣌ ؔ਺ऴྃ࣌ είʔϓ֎
    Executor Executor
    if true {
    let object: Object = Object()
    object.printWeakSelf()
    weakObject = object
    }
    6. Executor ͷΫϩʔδϟ಺Ͱ [weak self] Λ࢖͏৔߹

    View full-size slide

  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] Λ࢖Θͳ͍৔߹

    View full-size slide

  20. object
    closure
    object object
    closure
    object
    Executor Executor
    είʔϓ಺ ؔ਺࣮ߦ࣌ ؔ਺ऴྃ࣌ είʔϓ֎
    if true {
    let object: Object = Object()
    object.printSelf()
    weakObject = object
    }
    7. Executor ͷΫϩʔδϟ಺Ͱ [weak self] Λ࢖Θͳ͍৔߹

    View full-size slide

  21. Ϋϩʔδϟ಺Ͱ self Λࢀরͯ͠΋ϦʔΫ͠ͳ͍৔߹΋͋Δ

    View full-size slide

  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] Λ࢖͏৔߹

    View full-size slide

  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] Λ࢖͏৔߹

    View full-size slide

  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] Λ࢖Θͳ͍৔߹

    View full-size slide

  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] Λ࢖Θͳ͍৔߹

    View full-size slide

  26. ඇಉظॲཧͰ͸ίʔϧόοΫ࣌ʹ
    object ͕ੜଘ͍ͯ͠Δ͔Ͳ͏͔͕มΘͬͯ͘Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. Motivation / Concept
    https://developers.google.com/protocol-buffers/
    Template
    message

    View full-size slide