๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

ARC

  • ์ž๋™ ์ฐธ์กฐ ์นด์šดํŒ… Automatic Reference Counting
  • swift๋Š” ARC๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์„ ๋•Œ ์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์ค€๋‹ค.
  • ๋‹น์—ฐํ•˜๊ฒŒ๋„ struct์™€ enum์€ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” valueํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น๋˜์ง€ ์•Š๊ณ  ์ฐธ์กฐ ํƒ€์ž…์ธ class์˜ ์ธ์Šคํ„ด์Šค์—๋งŒ ์ฐธ์กฐ ์นด์šดํŒ…์ด ์ ์šฉ๋œ๋‹ค.

 

  • ์ž๋ฐ”์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰์…˜(GC)์™€ ๋‹ค๋ฅธ์ ์€ GC๋Š” ๋Ÿฐํƒ€์ž„๋•Œ ๊ณ„์† ์ฐธ์กฐ๋ฅผ ์ถ”์ ํ•˜๊ณ  RC๋Š” ์ปดํŒŒ์ผ๋•Œ ๋ชจ๋‘ ๊ฒฐ์ •์ด ๋‚œ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

 

ARC์˜ ์ž‘๋™ ์›๋ฆฌ

  • ํด๋ž˜์Šค์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ARC๋Š” ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์คŒ
  • ์ธ์Šคํ„ด์Šค๊ฐ€ ๋”์ด์ƒ ํ•„์š”์น˜ ์•Š์„ ๋•Œ ARC๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์ œ ํ•ด์คŒ
  • ์‚ฌ์šฉ์ค‘์ธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œํ•˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค์— ๊ฐ•ํ•œ ์ฐธ์กฐ (strong reference) ๋ฅผ ๋งŒ๋“ค์–ด ์ฐธ์กฐ ํšŸ์ˆ˜๊ฐ€ 0์ด ๋  ๋•Œ ํ•ด์ œ๋ฅผ ํ•จ

 

 

๊ฐ•ํ•œ ์ฐธ์กฐ ์‚ฌ์ดํด (Strong Reference Cycles)

  • ์ฐธ์กฐ ํšŸ์ˆ˜๊ฐ€ 0์ด ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ์— ํ•ด์ œ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ → ๊ฐ•ํ•œ ์ฐธ์กฐ ์‚ฌ์ดํด
class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
    deinit { print("\\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
    var tenant: Person?
    deinit { print("Apartment \\(unit) is being deinitialized") }
}

var john: Person?
var unit4A: Apartment?

john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")

john!.apartment = unit4A
unit4A!.tenant = john
  • Person์€ Apartment ์ธ์Šคํ„ด์Šค ์˜ต์…”๋„ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง
  • Apartment๋Š” Person ์ธ์Šคํ„ด์Šค ์˜ต์…”๋„ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง
  • john, unit4A ๋‘ ์ธ์Šคํ„ด์Šค๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐ์‹œํ‚ด → ๊ฐ•ํ•œ ์ฐธ์กฐ ์‚ฌ์ดํด ์ƒ์„ฑ

 

 

john = nil
unit4A = nil
  • ์ธ์Šคํ„ด์Šค ๊ฐ„์˜ ๊ฐ•ํ•œ ์ฐธ์กฐ๋Š” ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์•ˆ๋จ

 

 

๊ฐ•ํ•œ ์ฐธ์กฐ ์‚ฌ์ดํด ํ•ด๊ฒฐ - ์•ฝํ•œ ์ฐธ์กฐ

  • ์•ฝํ•œ ์ฐธ์กฐ๋ž€?
    • ์ฐธ์กฐํ•˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ํ• ๋‹น ํ•ด์ œ๋˜๋ฉด ์•ฝํ•œ ์ฐธ์กฐ๋ฅผ ํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ nil ๋กœ ์ž๋™์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
    • weak ํ‚ค์›Œ๋“œ
  • weak let ๋Š” ์•ˆ๋˜๊ณ  weak var ๋งŒ ๋˜๋Š” ์ด์œ ?
    • ์ธ์Šคํ„ด์Šค๊ฐ€ ํ• ๋‹น ํ•ด์ œ๋  ๋•Œ ์•ฝํ•œ ์ฐธ์กฐ๋ฉด ์ž๋™์œผ๋กœ nil๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
    • ์ฆ‰ ๋Ÿฐํƒ€์ž„์— ๊ฐ’์ด nil๋กœ ๋ฐ”๋€Œ๋Š”๊ฒƒ์„ ํ—ˆ๋ฝํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์ˆ˜๋Š” ์“ธ ์ˆ˜ ์—†๋‹ค.
class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
    deinit { print("\\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
    weak var tenant: Person? // ์•ฝํ•œ ์ฐธ์กฐ
    deinit { print("Apartment \\(unit) is being deinitialized") }
}

var john: Person?
var unit4A: Apartment?

john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")

john!.apartment = unit4A
unit4A!.tenant = john

 

 

 

john = nil
  • apartment ์ธ์Šคํ„ด์Šค๋Š” Person ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ์•ฝํ•œ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ์ฐธ์กฐํ•˜๋˜ john ์ด ํ•ด์ œ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ tenant ํ”„๋กœํผํ‹ฐ๋Š” nil ๋กœ ์„ค์ •๋œ๋‹ค.

 

 

๊ฐ•ํ•œ ์ฐธ์กฐ ์‚ฌ์ดํด ํ•ด๊ฒฐ - ๋ฏธ์†Œ์œ  ์ฐธ์กฐ

  • unowned ํ‚ค์›Œ๋“œ
  • ์•ฝํ•œ์ฐธ์กฐ๋ž‘ ๋‹ค๋ฅด๊ฒŒ ์˜ต์…”๋„์ด ์•„๋‹ˆ๊ณ  ํ•ญ์ƒ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • ์•ฝํ•œ ์ฐธ์กฐ์™€ ๋‹ค๋ฅด๊ฒŒ ๋ฏธ์†Œ์œ  ์ฐธ์กฐ๋Š” ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์˜ ์ˆ˜๋ช…์ด ๊ฐ™๊ฑฐ๋‚˜ ๋” ๊ธด ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.
class Customer {
    let name: String
    var card: CreditCard? // ์˜ต์…”๋„
    init(name: String) {
        self.name = name
    }
    deinit { print("\\(name) is being deinitialized") }
}

class CreditCard {
    let number: UInt64
    unowned let customer: Customer // ๋ฏธ์†Œ์œ  ์ฐธ์กฐ, ์˜ต์…”๋„ x
    init(number: UInt64, customer: Customer) {
        self.number = number
        self.customer = customer
    }
    deinit { print("Card #\\(number) is being deinitialized") }
}

var john: Customer?

john = Customer(name: "John Appleseed")
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)

john = nil
  • ๊ณ ๊ฐ์€ ํ•ญ์ƒ ์‹ ์šฉ์นด๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง„ ์•Š์ง€๋งŒ ์‹ ์šฉ์นด๋“œ๋Š” ํ•ญ์ƒ ๊ณ ๊ฐ์ด ์žˆ๋‹ค.
  • ๊ณ ๊ฐ์ด ์‹ ์šฉ์นด๋“œ๋ณด๋‹ค ๋” ์ธ์Šคํ„ด์Šค์˜ ์ˆ˜๋ช…์ด ๊ธธ๋‹ค๋Š” ํ™•์‹ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฏธ์†Œ์œ  ์ฐธ์กฐ ์‚ฌ์šฉ

 

 

 

์ฐธ๊ณ 

iOS) ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ (1/3) - ARC(Automatic Reference Counting)

 

iOS) ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ (1/3) - ARC(Automatic Reference Counting)

์•ˆ๋…•ํ•˜์„ธ์š”~~ ์†Œ๋“ค์ž…๋‹ˆ๋‹ค ๐Ÿ‘€ ์˜ค๋Š˜์€ ์ง€๋‚œ ์‹œ๊ฐ„ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์— ์ด์–ด Swift๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ๋˜๋Š”์ง€์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณผ ๊ฑฐ์˜ˆ์š” :) ARC ๋ฉด์ ‘ ๋‹จ๊ณจ ์งˆ๋ฌธ์ด๋ผ์ฃ ? ๊น”๊น” iOS ๊ฐœ๋ฐœ์ž๋ผ

babbab2.tistory.com

 

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...