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

weak-vs-unowned

Yutaro Muta
September 02, 2018

 weak-vs-unowned

Yutaro Muta

September 02, 2018
Tweet

More Decks by Yutaro Muta

Other Decks in Technology

Transcript

  1. weak vs unowned
    2018/09/02 @iOSDC Japan 2018
    Yutaro Muta @yutailang0119

    View full-size slide

  2. • Yutaro Muta @yutailang0119
    • Hatena Co., Ltd. @Kyoto
    • Conference Staff
    • builderscon 2017, 2018
    • PyCon JP 2016, 2017
    • and more

    Who am I ?

    View full-size slide

  3. https://fortee.jp/iosdc-japan-2018/proposal/b84477b0-a13b-4994-a250-ad2300229e83

    View full-size slide

  4. Goal
    • Ͳ͏࢖͍෼͚Δ΂͖͔Λ஌Δ

    View full-size slide

  5. Agenda
    • ॥؀ࢀরͷ෮श
    • Strong, Weak, Unowned
    • WeakͷϝϦοτ/σϝϦοτ
    • UnownedͷϝϦοτ/σϝϦοτ
    • ·ͱΊ

    View full-size slide

  6. ॥؀ࢀরͷ෮श

    View full-size slide

  7. ਓྨ͸ɺ͍͔ͭΒ೰·͞Ε͍ͯΔͷ͔

    View full-size slide

  8. https://qiita.com/susieyy/items/39efd4d34cf4c1f9d3ef

    View full-size slide

  9. Sample
    https://github.com/yutailang0119/iOSDC2018Sample

    View full-size slide

  10. pOBMDMBTT1BSFOU7JFX$POUSPMMFS6*7JFX$POUSPMMFS\
    QSJWBUFGVODBEE$IJME7JFX CZSFGFSFODF5ZQF3FGFSFODF5ZQF
    \
    MFUTUPSZCPBSE6*4UPSZCPBSE OBNF$POUBJOFS4BNQMF CVOEMFOJM

    MFUDIJME7JFX$POUSPMMFSTUPSZCPBSEJOTUBOUJBUF7JFX$POUSPMMFS XJUI*EFOUJpFS$IJME
    BT$IJME7JFX$POUSPMMFS
    MFUDMPTVSF 4USJOH
    7PJE
    TXJUDISFGFSFODF5ZQF\
    DBTFTUSPOH
    DMPTVSF\UFYUJO
    TFMGUFYU-BCFMUFYUUFYU
    ^
    DBTFXFBL
    DMPTVSF\UFYUJO
    TFMG UFYU-BCFMUFYUUFYU
    ^
    DBTFVOPXOFE
    DMPTVSF\UFYUJO
    TFMGUFYU-BCFMUFYUUFYU
    ^
    ^
    DIJME7JFX$POUSPMMFSSFGFSFODF5ZQFSFGFSFODF5ZQF
    DIJME7JFX$POUSPMMFSUBQ$MPTVSFDMPTVSF
    BEE$IJME7JFX$POUSPMMFS DIJME7JFX$POUSPMMFS

    DIJME7JFX$POUSPMMFSWJFXGSBNFDPOUBJOFS7JFXCPVOET
    DPOUBJOFS7JFXBEE4VCWJFX DIJME7JFX$POUSPMMFSWJFX

    DIJME7JFX$POUSPMMFSEJE.PWF UP1BSFOU7JFX$POUSPMMFSTFMG

    ^
    ^
    ॥؀ࢀরͷ෮श

    View full-size slide

  11. Strong Reference

    View full-size slide

  12. Weak Reference

    View full-size slide

  13. Unowned Reference

    View full-size slide

  14. pOBMDMBTT4USPOH7JFX$POUSPMMFS6*7JFX$POUSPMMFS\
    !*#"DUJPOGVODUBQ4USPOH @TFOEFS6*#VUUPO
    \
    MFUBQJ$MJFOU63-4FTTJPO$MJFOU63-4FTTJPO$MJFOU

    MFUSFGFSFODF5ZQF3FGFSFODF5ZQFTUSPOH
    BQJ$MJFOUHFU VSMIUUQTIUUQCJOPSHHFU
    RVFSZ*UFNTSFGFSFODF5ZQFRVFSZ*UFNT
    \SFTQPOTFJO
    TFMGSFMPBE7JFX CZSFTQPOTF

    ^
    ^
    ^
    ॥؀ࢀর͠ͳ͍ύλʔϯ

    View full-size slide

  15. Strong ReferenceͰ΋॥؀ࢀরͯ͠ͳ͍

    View full-size slide

  16. ༨ஊ: weakΩϟϓνϟ͸ self Ҏ֎΋Ͱ͖Δ
    pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\
    !*#0VUMFUXFBLWBSVSM-BCFM6*-BCFM
    !*#0VUMFUXFBLWBSRVFSZ*UFNT-BCFM6*-BCFM
    !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO
    \
    SFRVFTU CZXFBL
    \SFTQPOTFJO
    MFUQBSTFE3FTQPOTF3FTQPOTF SFTQPOTFSFTQPOTF

    %JTQBUDI2VFVFNBJOBTZOD\
    VSM-BCFM UFYUQBSTFE3FTQPOTF VSM
    RVFSZ*UFNT-BCFM UFYUQBSTFE3FTQPOTF BSHT
    ^
    ^
    ^
    ^
    ※ͨͩ͠ɺunowned͸Ͱ͖ͳ͍
    `property` may only be applied to class and class-bound protocol types, not ‘Type'

    View full-size slide

  17. WeakͷϝϦοτ/σϝϦοτ

    View full-size slide

  18. WeakͷϝϦοτ/σϝϦοτ
    • ϝϦοτ
    • ࣮֬ʹΫϥογϡΛ๷͛Δ
    • σϝϦοτ
    • ৑௕ʹͳΔ͜ͱ͕͋Δ

    View full-size slide

  19. pOBMDMBTT$SBTI7JFX$POUSPMMFS6*7JFX$POUSPMMFS\
    !*#"DUJPOGVODUBQ8FBL @TFOEFS6*#VUUPO
    \
    SFRVFTU CZXFBL
    \SFTQPOTFJO
    %JTQBUDI2VFVFNBJOBTZOD"GUFS EFBEMJOF%JTQBUDI5JNFOPX

    \
    TFMG SFMPBE7JFX CZSFTQPOTF

    ^
    ^
    OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF

    ^
    !*#"DUJPOGVODUBQ6OPXOFE @TFOEFS6*#VUUPO
    \
    SFRVFTU CZVOPXOFE
    \SFTQPOTFJO
    %JTQBUDI2VFVFNBJOBTZOD"GUFS EFBEMJOF%JTQBUDI5JNFOPX

    \
    TFMGSFMPBE7JFX CZSFTQPOTF

    ^
    ^
    OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF

    ^
    ^
    ϝϦοτ: UnownedͰΫϥογϡ͢Δঢ়گ΋ճආͰ͖Δ

    View full-size slide

  20. σϝϦοτ: ৑௕ʹͳΔ͜ͱ͕͋Δ
    pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\
    QSJWBUFMFUOBNFZVUBJMBOH
    !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO
    \
    SFRVFTU CZXFBL
    \@JO
    HVBSEMFUTUSPOH4FMGTFMGFMTF\
    SFUVSO
    ^
    QSJOU TUSPOH4FMGOBNF

    ^
    ^
    ^
    if let ΍ guard let Ͱ৑௕ʹΞΫηε੍ޚ͢Δඞཁ͕ग़ͨΓ͢Δ
    ಛʹ self ͸Shadowingͷ໰୊͕͋Δ

    View full-size slide

  21. UnownedͷϝϦοτ/σϝϦοτ

    View full-size slide

  22. UnownedͷϝϦοτ/σϝϦοτ
    • ϝϦοτ
    • Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δ
    • ΞΫηε࣌ʹnilͰͳ͍͜ͱΛ໌ࣔͰ͖Δ (อূ͸Ͱ͖ͳ͍)
    • σϝϦοτ
    • ద੾ʹ࢖༻͠ͳ͚Ε͹ɺΫϥογϡ͢Δ͜ͱ͕͋Δ

    View full-size slide

  23. UnownedͷϝϦοτ/σϝϦοτ
    • ϝϦοτ
    • Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δ
    • ΞΫηε࣌ʹnilͰͳ͍͜ͱΛ໌ࣔͰ͖Δ (อূ͸Ͱ͖ͳ͍)
    • σϝϦοτ
    • ద੾ʹ࢖༻͠ͳ͚Ε͹ɺΫϥογϡ͢Δ͜ͱ͕͋Δ

    View full-size slide

  24. Copyright © 2018 Apple Inc. All rights reserved.
    https://help.apple.com/instruments/mac/current/
    ݕূ: Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δͷ͔

    View full-size slide

  25. ݕূ: Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δͷ͔

    View full-size slide

  26. ࠓճͷαϯϓϧϨϕϧͰ͸ɺ
    ༏Ґͳࠩ͸ݟΒΕͳ͔ͬͨ
    -> ଞͷύϑΥʔϚϯεΛ
    ޲্ͤͨ͞ํ͕ے͕͍͍

    View full-size slide

  27. Stack Overflow
    weak vs unowned in Swift. What are the internal differences?
    https://stackoverflow.com/a/42847825

    View full-size slide

  28. Stack Overflow
    weak vs unowned in Swift. What are the internal differences?
    https://stackoverflow.com/a/42847825
    > In my usage, the common case arises in situations where a closure needs a capture list
    involving self in order to avoid a retain cycle. In such a situation, it is almost always
    possible to say [unowned self] in the capture list. When we do:
    > * It is more convenient for the programmer because there is nothing to unwrap. [weak
    self] would be an Optional in need of unwrapping in order to use it.
    > * It is more efficient, partly for the same reason (unwrapping always adds an extra level
    of indirection) and partly because it is one fewer weak reference for the runtime's
    scratchpad list to keep track of.

    View full-size slide

  29. Stack Overflow
    weak vs unowned in Swift. What are the internal differences?
    https://stackoverflow.com/a/42847825
    > In my usage, the common case arises in situations where a closure needs a capture list
    involving self in order to avoid a retain cycle. In such a situation, it is almost always
    possible to say [unowned self] in the capture list. When we do:
    > * It is more convenient for the programmer because there is nothing to unwrap. [weak
    self] would be an Optional in need of unwrapping in order to use it.
    > * It is more efficient, partly for the same reason (unwrapping always adds an extra level
    of indirection) and partly because it is one fewer weak reference for the runtime's
    scratchpad list to keep track of.

    View full-size slide

  30. UnownedͷϝϦοτ/σϝϦοτ
    • ϝϦοτ
    • Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δ
    • ΞΫηε࣌ʹOJMͰͳ͍͜ͱΛ໌ࣔͰ͖Δ (อূ͸Ͱ͖ͳ͍)
    • σϝϦοτ
    • ద੾ʹ࢖༻͠ͳ͚Ε͹ɺΫϥογϡ͢Δ͜ͱ͕͋Δ

    View full-size slide

  31. The Swift Programing Language
    Automatic Reference Counting / Unowned References
    > Unlike a weak reference, however, an unowned reference is used when
    the other instance has the same lifetime or a longer lifetime. You indicate
    an unowned reference by placing the unowned keyword before a property
    or variable declaration.
    > An unowned reference is expected to always have a value. As a result,
    ARC never sets an unowned reference’s value to nil, which means that
    unowned references are defined using nonoptional types.
    https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html

    View full-size slide

  32. The Swift Programing Language
    Automatic Reference Counting / Unowned References
    > Unlike a weak reference, however, an unowned reference is used when
    the other instance has the same lifetime or a longer lifetime. You indicate
    an unowned reference by placing the unowned keyword before a property
    or variable declaration.
    > An unowned reference is expected to always have a value. As a result,
    ARC never sets an unowned reference’s value to nil, which means that
    unowned references are defined using nonoptional types.
    https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html

    View full-size slide

  33. ϓϩάϥϚͷҙਤ͕ٴͿൣғͰ͸ɺ
    ࿦ཧ͕ͳΔ΂͘ίʔυʹ
    ൓ө͞Ε͍ͯΔํ͕๬·͍͠

    View full-size slide

  34. Ͳ͏࢖͍෼͚Δ΂͖͔
    • ϝϞϦղ์ͷλΠϛϯά͕ෆ໌ͳ৔߹͸ɺweakΛ࢖༻
    • ࣮ߦ࣌ͷڍಈΛཧղͰ͖Δࣗ৴͕ͳ͍৔߹͸ɺweak͕ແ೉
    • γϯϓϧͳΞϓϦέʔγϣϯͰ͸ɺύϑΥʔϚϯεʹࠩ͸ग़ʹ͍͘
    ͷͰɺ҆৺ײΛಘΔͨΊͷweak͸͋Γͱݴ͑Δ
    • ϝϞϦղ์͕ߦΘΕͳ͍͜ͱ͕લఏͷॲཧͰɺ·ͨͦΕΛอূͰ͖Δ৔
    ߹͸ɺunownedΛ࢖͏

    View full-size slide

  35. ओ؍
    Swift͕weakͱunownedͷ2ͭΛఏڙ͍ͯ͠ΔͷͳΒɺ
    ϓϩάϥϚͷҙ͕ࣝհࡏ͢ΔίʔυʹͳΔΑ͏ʹ
    ࢖͍෼͚͍ͨ

    View full-size slide

  36. Reference
    • https://qiita.com/susieyy/items/39efd4d34cf4c1f9d3ef
    • https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
    • https://github.com/yutailang0119/iOSDC2018Sample
    • https://help.apple.com/instruments/mac/current/
    • https://stackoverflow.com/questions/42842336/weak-vs-unowned-in-swift-what-are-
    the-internal-differences
    • http://www.apeth.com/iOSBook/ch12.html#_property_memory_management_policies

    View full-size slide

  37. ͸ͯͳͰ͸ɺ͜Μͳݴ༿͕ࢗ͞ΔਓΛืू͍ͯ͠·͢ʂ
    ϓϩάϥϚͷҙਤ͕ٴͿൣғͰ͸ɺ
    ࿦ཧ͕ͳΔ΂͘ίʔυʹ
    ൓ө͞Ε͍ͯΔํ͕๬·͍͠

    View full-size slide

  38. &OKPZJ04%$ 5IBOLT
    w NVUBZVUBSP!HNBJMDPN
    w IUUQTUXJUUFSDPNZVUBJMBOH
    w IUUQTHJUIVCDPNZVUBJMBOH

    View full-size slide