์์ :
https://developer.apple.com/videos/play/wwdc2021/10216/
ARC in Swift: Basics and beyond - WWDC21 - Videos - Apple Developer
Learn about the basics of object lifetimes and ARC in Swift. Dive deep into what language features make object lifetimes observable,...
developer.apple.com
Object lifetimes and ARC
traveler1์ ๋ง์ง๋ง ์ฌ์ฉ ํ์ release operation์ ์ํ traveler1์ Retain operation์ ์ํ๋์ง ์๋๋ฐ ์๋ํ๋ฉด ์ด๊ธฐํ์ reference count๊ฐ 1๋ก ์ค์ ๋์๊ธฐ ๋๋ฌธ
Traveler2 ์ ๋ํด์ retain๊ณผ release๊ฐ ๋ถ์
๊ทธ๋ฆฌ๊ณ traveler2์ ๋ง์ง๋ง destination ํ์ ๋ฐ๋ก deallocated๋จ
ํ์ง๋ง weak, unowned ์ฌ์ฉ์ ๋ฐ๋ผ object์ lifetime์ ๊ด์ฐฐ(observable)ํ๋ ๊ฒ์ด ์ด๋ ค์ธ ์๋ ์์
๋ง์ฝ ๊ด์ฐฐ๋ ๊ฐ์ฒด์ lifetime์ ๋์ ๋ณด์ฅํ๊ธฐ๋ณด๋ค ๊ฐ์ฒด์ lifetime ํ๋ฆ์ ์์กดํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ ๊ฒฝ์ฐ, ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์๋ ์๋ค.
๊ฐ์ฒด์ lifetime ํ๋ฆ์ ์์กดํ๋ฉด์ ์๊ธฐ๋ ๋ฌธ์ ๋ ๊ฐ๋ฐ๋์์๋ ๋ฐ๊ฒฌ๋์ง ์์์ง๋ ๋ชจ๋ฅด๊ณ ์จ๊ฒจ์ง๊ฑฐ๋ ์์ฃผ ๋์ค์ ๋ฐ์ํ ํ๋ฅ ์ด ๋๋ค. (compiler๊ฐ ์ต์ ํ๋์ด ์
๋ฐ์ดํธ ๋ ๋, ๊ด๋ จ ์๋ ์์ค์ฝ๋๊ฐ ๋ณ๊ฒฝ๋ ๋)
Observable Object lifetime
weak์ unowned๋ reference cycle์ ์์ ๋๋ฐ ํจ๊ณผ์
account๊ฐ์ฒด๊ฐ ์์ฑ์ traveler์ ref_count๊ฐ 2๋ก ์ฆ๊ฐ๋๋ค.
traveler.account ์ account์ ref_count๋ 1 ์ฆ๊ฐํ์ฌ 2๊ฐ ๋๊ณ release์ ์ํด ๋ค์ 1๋ก ๊ฐ์ํ๋ค.
Traveler.printSummary()์คํ์ ref_count๊ฐ ๊ฐ์ํ๋ค.
๋ชจ๋ ์ฝ๋์ ์ฌ์ฉ์ด ๋๋ฌ์์๋ ref_count๊ฐ 1๋ก ์๋์ผ๋ก deallocate๋์ง ์๊ณ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๋ค.
ํ์ง๋ง
weak๋ก ์ ์ธ์ account๊ฐ์ฒด๊ฐ ์์ฑ์ traveler์ ref_count๊ฐ ์ฆ๊ฐ๋์ง ์์ผ๋ฏ๋ก account.printSummary() ์คํ ํ 0์ด ๋์ด dealloc๋ ์ ์๋ค.
ํ์ง๋ง ๋ณด์ฅ๋ ๊ฐ์ฒด ์๋ช
์ด ์ข
๋ฃ๋ ๋์ weak ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์ ๊ทผํ๊ณ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด Observable ๊ฐ์ฒด lifetime์ ์์กดํ๋ ๊ฒฝ์ฐ, Observable ๊ฐ์ฒด lifetime์ด ๊ด๋ จ ์๋ ๊ฐ์ฒด์ ๋ํด ๋ณ๊ฒฝ๋ ๋ ๋ฏธ๋์ ๋ฒ๊ทธ๋ก ๋๋ ์ ์๋ค.
Traveler์ account๊ฐ ๋๋๊ณ Traveler ๊ฐ dealloc๋ ๋ account๊ฐ์ฒด์ traveler๋ Nil์ด ๋ ๊ฒ์ด๋ค.
ํ์ account.printSummary()๊ฐ ์คํ์ ๊ฐ์ ๋ก unwrappingํ ๋ณ์์ ๋ํด crash๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
์ด๋ ์ต์
๋ ๋ฐ์ธ๋ฉ์ ํ ์ ์๋๋ฐ ๋ ์ต์
์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์จ๋ค. ์๋ฌด๋ฐ ์กฐ์น๊ฐ ๋์ง ์์ slient bug๊ฐ ๋ ๊ฒ์ด๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ ์ ์ํ๋ค.
withExtendLifetime()
๊ฐ์ฒด์ ๋ํ ์๋ช
์ ๋ช
์์ ์ผ๋ก ์ฆ๊ฐ์ํจ๋ค.
๋๋ ์๋์ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
ํ์ง๋ง ๋งค์ฐ ์ฐ์ฝํ๋ค !
์ฝ๋ ์ฌ์ฉํ๋ ์ฌ์ฉ์์ ์ฑ
์์ ์์กด์ ์ด๊ธฐ ๋๋ฌธ
๋ฆฌ๋์์ธ ๊ฒฐ๊ณผ
๋๋ ํธ๋ฆฌ ๊ตฌ์กฐ์ ๋ค๋ฅธ struct๋ฅผ ๋ง๋ค์ด ์ํ์ฐธ์กฐ๋ฅผ ํผํ ์๋ ์๋ค.
deinitializer๋ deallocate ์ ์ ํธ์ถ๋๋๋ฐ ์ด๊ฒ์ ๋ํด ๋ถ์์ฉ์ผ๋ก ์ธ๋ถ ํ๋ก๊ทธ๋จ์ ์ํฅ์ ์ค ์ ์๋ค. ๋๋ ์จ๊ฒจ์ง ๋ฒ๊ทธ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฉฐ, ์ด๋ ๊ด๋ จ ์๋ ์ด์ ๋ก ๊ด์ฐฐ๋ ๊ฐ์ฒด ์๋ช
์ด ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ์๋ง ๋ฐ๊ฒฌ๋ฉ๋๋ค.
deinitializer๋ globaํ ๋ถ์์ฉ(์ฝ์ ์ถ๋ ฅ)์ ๊ฐ์ง๋ค.
ํ์ฌ์ deinitializer๋ “Done traveling”์ถ๋ ฅ ํ ์คํ๋์ง๋ง ARC ์ต์ ํ์ ๋ฐ๋ผ destination์ด release๋ ํ deinit๋๋ค.
deinit์ metrics์ publish๊ฐ ํธ์ถ๋๋ค.
Traveler.updateDestination()์ ์คํ ํ dealloc๋๊ธฐ ์ deinit์ด ํธ์ถ๋๋๋ฐ
์๋ computeTravel์ ๊ณ์ฐํ๊ธฐ ์ ์ deinit๋๋ฏ๋ก
publish() ํธ์ถ์ category๊ฐ์ด nil๋ก ๋จ๊ฒ ๋๋ ๋ฌธ์ ์ ์ด ์๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด -> withExtendLifetime
private์ผ๋ก ์จ๊น -> test()ํจ์์์ ์ฌ์ฉ ํ ์ ์๊ฒ๋, then -> deinit์์ computeTravel, publish ์ํ
๋๋ defer๋ฅผ ์ฌ์ฉํด publish, deinit์ ํ์ธ๋ง ์ํํ๋๋ก ๊ตฌํ
on์ ์ฆ์ dealloc๋ ๊ฒ ์ฆ ๊ฐ๋ ฅํ ์๋ช ๋จ์ถ ARC์ต์ ํ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํจ ๊ธฐ๋ฅ ์ถ๊ฐ
'IOS๐ > WWDC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[WWDC 2021] Your guide to keyboard layout (0) | 2022.05.26 |
---|---|
[WWDC 2021] Protect mutable state with Swift actors (0) | 2022.05.24 |
[WWDC 2021] Meet async/await in Swift (0) | 2022.05.17 |
[WWDC 2020] Advances in diffable data sources (0) | 2022.05.12 |
[WWDC 2019] Advances in UI Data Sources (0) | 2022.05.10 |
๋๊ธ