๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/WWDC

[WWDC 2021] ARC in Swift: Basics and beyond (ARC ์ตœ์ ํ™”)

by Jouureee 2022. 4. 16.

์˜์ƒ :

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

๋Œ“๊ธ€