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

weak-vs-unowned

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=47 Yutaro Muta
September 02, 2018

 weak-vs-unowned

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=128

Yutaro Muta

September 02, 2018
Tweet

Transcript

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

  2. • Yutaro Muta @yutailang0119 • Hatena Co., Ltd. @Kyoto •

    Conference Staff • builderscon 2017, 2018 • PyCon JP 2016, 2017 • and more • Who am I ?
  3. https://fortee.jp/iosdc-japan-2018/proposal/b84477b0-a13b-4994-a250-ad2300229e83

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

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

    UnownedͷϝϦοτ/σϝϦοτ • ·ͱΊ
  6. ॥؀ࢀরͷ෮श

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

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

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

  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\<XFBLTFMG>UFYUJO TFMG UFYU-BCFMUFYUUFYU ^ DBTFVOPXOFE DMPTVSF\<VOPXOFETFMG>UFYUJO TFMGUFYU-BCFMUFYUUFYU ^ ^ DIJME7JFX$POUSPMMFSSFGFSFODF5ZQFSFGFSFODF5ZQF DIJME7JFX$POUSPMMFSUBQ$MPTVSFDMPTVSF BEE$IJME7JFX$POUSPMMFS DIJME7JFX$POUSPMMFS DIJME7JFX$POUSPMMFSWJFXGSBNFDPOUBJOFS7JFXCPVOET DPOUBJOFS7JFXBEE4VCWJFX DIJME7JFX$POUSPMMFSWJFX DIJME7JFX$POUSPMMFSEJE.PWF UP1BSFOU7JFX$POUSPMMFSTFMG ^ ^ ॥؀ࢀরͷ෮श
  11. Strong Reference

  12. Weak Reference

  13. Unowned Reference

  14. pOBMDMBTT4USPOH7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ !*#"DUJPOGVODUBQ4USPOH @TFOEFS6*#VUUPO \ MFUBQJ$MJFOU63-4FTTJPO$MJFOU63-4FTTJPO$MJFOU MFUSFGFSFODF5ZQF3FGFSFODF5ZQFTUSPOH BQJ$MJFOUHFU VSMIUUQTIUUQCJOPSHHFU RVFSZ*UFNTSFGFSFODF5ZQFRVFSZ*UFNT \SFTQPOTFJO

    TFMGSFMPBE7JFX CZSFTQPOTF ^ ^ ^ ॥؀ࢀর͠ͳ͍ύλʔϯ
  15. Strong ReferenceͰ΋॥؀ࢀরͯ͠ͳ͍

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

    SFRVFTU CZXFBL \<XFBLVSM-BCFM XFBLRVFSZ*UFNT-BCFM>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'
  17. WeakͷϝϦοτ/σϝϦοτ

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

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

     \ TFMG SFMPBE7JFX CZSFTQPOTF ^ ^ OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF ^ !*#"DUJPOGVODUBQ6OPXOFE @TFOEFS6*#VUUPO \ SFRVFTU CZVOPXOFE \<VOPXOFETFMG>SFTQPOTFJO %JTQBUDI2VFVFNBJOBTZOD"GUFS EFBEMJOF%JTQBUDI5JNFOPX   \ TFMGSFMPBE7JFX CZSFTQPOTF ^ ^ OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF ^ ^ ϝϦοτ: UnownedͰΫϥογϡ͢Δঢ়گ΋ճආͰ͖Δ
  20. σϝϦοτ: ৑௕ʹͳΔ͜ͱ͕͋Δ pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ QSJWBUFMFUOBNFZVUBJMBOH !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO \ SFRVFTU CZXFBL \<XFBLTFMG>@JO

    HVBSEMFUTUSPOH4FMGTFMGFMTF\ SFUVSO ^ QSJOU TUSPOH4FMGOBNF ^ ^ ^ if let ΍ guard let Ͱ৑௕ʹΞΫηε੍ޚ͢Δඞཁ͕ग़ͨΓ͢Δ ಛʹ self ͸Shadowingͷ໰୊͕͋Δ
  21. UnownedͷϝϦοτ/σϝϦοτ

  22. UnownedͷϝϦοτ/σϝϦοτ • ϝϦοτ • Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δ • ΞΫηε࣌ʹnilͰͳ͍͜ͱΛ໌ࣔͰ͖Δ (อূ͸Ͱ͖ͳ͍) • σϝϦοτ

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

    • ద੾ʹ࢖༻͠ͳ͚Ε͹ɺΫϥογϡ͢Δ͜ͱ͕͋Δ
  24. Copyright © 2018 Apple Inc. All rights reserved. https://help.apple.com/instruments/mac/current/ ݕূ:

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

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

  28. Stack Overflow weak vs unowned in Swift. What are the

    internal differences? https://stackoverflow.com/a/42847825
  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.
  30. 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.
  31. UnownedͷϝϦοτ/σϝϦοτ • ϝϦοτ • Weakʹൺ΂ͯύϑΥʔϚϯε͕༏ҐͰ͋Δ • ΞΫηε࣌ʹOJMͰͳ͍͜ͱΛ໌ࣔͰ͖Δ (อূ͸Ͱ͖ͳ͍) • σϝϦοτ

    • ద੾ʹ࢖༻͠ͳ͚Ε͹ɺΫϥογϡ͢Δ͜ͱ͕͋Δ
  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
  33. 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
  34. ϓϩάϥϚͷҙਤ͕ٴͿൣғͰ͸ɺ ࿦ཧ͕ͳΔ΂͘ίʔυʹ ൓ө͞Ε͍ͯΔํ͕๬·͍͠

  35. ·ͱΊ

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

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

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

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