Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

॥؀ࢀরͷ෮श

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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 ^ ^ ॥؀ࢀরͷ෮श

Slide 11

Slide 11 text

Strong Reference

Slide 12

Slide 12 text

Weak Reference

Slide 13

Slide 13 text

Unowned Reference

Slide 14

Slide 14 text

pOBMDMBTT4USPOH7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ !*#"DUJPOGVODUBQ4USPOH @TFOEFS6*#VUUPO \ MFUBQJ$MJFOU63-4FTTJPO$MJFOU63-4FTTJPO$MJFOU MFUSFGFSFODF5ZQF3FGFSFODF5ZQFTUSPOH BQJ$MJFOUHFU VSMIUUQTIUUQCJOPSHHFU RVFSZ*UFNTSFGFSFODF5ZQFRVFSZ*UFNT \SFTQPOTFJO TFMGSFMPBE7JFX CZSFTQPOTF ^ ^ ^ ॥؀ࢀর͠ͳ͍ύλʔϯ

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

༨ஊ: 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'

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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ͰΫϥογϡ͢Δঢ়گ΋ճආͰ͖Δ

Slide 20

Slide 20 text

σϝϦοτ: ৑௕ʹͳΔ͜ͱ͕͋Δ pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ QSJWBUFMFUOBNFZVUBJMBOH !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO \ SFRVFTU CZXFBL \@JO HVBSEMFUTUSPOH4FMGTFMGFMTF\ SFUVSO ^ QSJOU TUSPOH4FMGOBNF ^ ^ ^ if let ΍ guard let Ͱ৑௕ʹΞΫηε੍ޚ͢Δඞཁ͕ग़ͨΓ͢Δ ಛʹ self ͸Shadowingͷ໰୊͕͋Δ

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

·ͱΊ

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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