ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [WWDC 21] ARC in Swift: Basics and beyond
    iOS/πŸ‘¨πŸ»‍πŸ’» WWDC 와 ν…Œν¬μ„Έλ―Έλ‚˜ 2022. 5. 15. 17:35

    wwdc 21

     

    ARC in Swift: Basics and beyond

     

    μš”μ•½ πŸ‘€

    ARC λŠ” 였브젝트(μΈμŠ€ν„΄μŠ€)의 life time이 끝날 λ•Œ deallocate ν•΄μ€€λ‹€.

    κ·Έλ ‡λ‹€λ©΄ ARC λŠ” 였브젝트의 life time 이 λλ‚œμ§€ μ–΄λ–»κ²Œ μ•Œμˆ˜ μžˆλŠ” κ²ƒμΌκΉŒ? Referecne count λ₯Ό μΆ”μ ν•œλ‹€.

    ARC μ΅œμ ν™”μ— μ˜ν•˜μ—¬, observed obejct life time  κ³Ό guaranteed mininum object life time 이 λ‹€λ₯Ό 수 μžˆλ‹€.

    Observed object life time 은 λ―Έλž˜μ— μŠ€μœ„ν”„νŠΈ 컴파일러 κ΅¬ν˜„μ— 따라 변동 될 수 μžˆμœΌλ‹ˆ, 이것에 μ˜μ‘΄ν•˜λŠ” μ½”λ“œλŠ” 잠재적 버그λ₯Ό ν¬ν•¨ν•œ κ²ƒμ΄λ―€λ‘œ κ°€λŠ₯ν•œ μˆ˜μ •ν•΄μ•Όν•œλ‹€.

     

    ν‚€μ›Œλ“œ 쀑심 정리 πŸ’Ž

    object life time

    μŠ€μœ„ν”„νŠΈμ—μ„œ object life time 은 μ‚¬μš© 기반 ( use-based )  μ΄λ‹€.

    μ‚¬μš©(use) μ΄λž€ reference λ₯Ό 톡해 였브젝트의 속성에 μ ‘κ·Όν•˜κ±°λ‚˜, λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” ν–‰μœ„λ₯Ό ν¬ν•¨ν•œλ‹€.

    졜초의 μ‚¬μš©μ€ init() μ—μ„œ μ‹œμž‘λœλ‹€.

    init() λ™μž‘μ€ νž™μ— 였브젝트λ₯Ό μƒμ„±ν•˜λ©°, reference count λ₯Ό 1둜 μ„€μ •ν•œλ‹€.

    reference λ₯Ό λ§ˆμ§€λ§‰μœΌλ‘œ μ‚¬μš©ν•œ 직후, 였브젝트의 life time 은 μ’…λ£Œλœλ‹€.

    reference copy  λ™μž‘은 였브젝트의 μƒˆλ‘œμš΄ reference λ₯Ό λ§Œλ“€λ©°, reference count λ₯Ό 1 μ¦κ°€μ‹œν‚¨λ‹€.

    let obejct1 = SomeObject()
    let object2 = object1 //reference copy
    ...

     

    retain, release

    μŠ€μœ„ν”„νŠΈ μ»΄νŒŒμΌλŸ¬κ°€  reference μ‚¬μš© 기반으둜 μ½”λ“œμ— μœ„ λͺ…령어듀을 μ‚½μž…ν•œλ‹€.

    μƒˆλ‘œμš΄ reference κ°€ μ‚¬μš© μ‹œμž‘λ˜κΈ° 직전, μŠ€μœ„ν”„νŠΈ μ»΄νŒŒμΌλŸ¬λŠ” retain λͺ…λ Ήμ–΄λ₯Ό μΆ”κ°€ν•œλ‹€.

    retain λͺ…λ Ήμ–΄λŠ” 였브젝트의 reference count λ₯Ό 1 μ¦κ°€μ‹œν‚¨λ‹€.

     

    reference λ₯Ό λ§ˆμ§€λ§‰μœΌλ‘œ μ‚¬μš©ν•œ 직후, μŠ€μœ„ν”„νŠΈ μ»΄νŒŒμΌλŸ¬λŠ” release λͺ…λ Ήμ–΄λ₯Ό μΆ”κ°€ν•œλ‹€.

    release λͺ…λ Ήμ–΄λŠ” 였브젝트의 reference count λ₯Ό 1 κ°μ†Œμ‹œν‚¨λ‹€.

    단, init() 은 였브젝트의 refCount λ₯Ό 1둜 μ„€μ •ν•˜λ―€λ‘œ λ”°λ‘œ retain λͺ…λ Ήμ–΄κ°€ μΆ”κ°€λ˜μ§€ μ•ŠλŠ”λ‹€.

     

    {
      let obejct1 = SomeObject()
      
      retain // object2의 reference κ°€ μ‹œμž‘λ˜λ―€λ‘œ, μ»΄νŒŒμΌλŸ¬κ°€ μΆ”κ°€ν•œλ‹€.
      
      let object2 = object1 //reference copy
      
      release // object1 의 reference κ°€ μ’…λ£Œλ˜μ—ˆλ‹€.
      
      object2.someMethod()
      
      release // object2 의 reference κ°€ μ’…λ£Œλ˜μ—ˆλ‹€. 
    }

     

    observed life time

    ARC 의 μ΅œμ ν™” λ“±μ˜ 이유둜 변동적이닀.

    이것에 μ˜μ‘΄ν•˜λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ 잠재적 버그λ₯Ό ν¬ν•¨ν•˜λŠ” 것이닀.

    weak, unowned λ₯Ό μ‚¬μš©ν•˜λ©΄ κ²½μš°μ— 따라 observed life time 에 μ˜μ‘΄ν•˜κ²Œ λœλ‹€.

    항상 strong refernce λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 κ°€μž₯ λͺ…ν™•ν•œ 해결법이닀.

    {
      let obejct1 = SomeObject()
      
      object1.someMethod()
     
      release -------- guaranteed life time --------
      
      // ν•˜μ§€λ§Œ ARC μ΅œμ ν™”μ— μ˜ν•΄ 아직 object1 이 deallocated λ˜μ§€ μ•Šμ„ 수 있고,
      
      // λ”°λΌμ„œ 이 μ‹œμ μ— object1 의 λ©”λͺ¨λ¦¬ 접근이 κ°€λŠ₯ν•  수 μžˆλ‹€.
      
      // 이 보μž₯λ˜μ§€ μ•Šμ€ μ ‘κ·Ό κ°€λŠ₯ μ‹œκ°„μ„ observed life time 이라고 ν•œλ‹€.
    }

     

    guranteed minimum life time

    initialization λΆ€ν„° μ˜€λΈŒμ νŠΈμ— λŒ€ν•œ λͺ¨λ“  레퍼런슀의 λ§ˆμ§€λ§‰ μ‚¬μš© μ‹œμ κΉŒμ§€λ₯Ό 가리킨닀.

    이 life time 은 고정적이기 λ•Œλ¬Έμ— 여기에 μ˜μ‘΄ν•˜λŠ” 것은 λ¬Έμ œκ°€ μ—†λ‹€.

    {
      let obejct1 = SomeObject()
      
      object1.someMethod()
     
      release -------- guaranteed life time --------
      
      // reference 의 λ§ˆμ§€λ§‰ μ‚¬μš© 직후 release 되며, object1 의 life time 은 μ’…λ£Œλœλ‹€.
    }

     

    weak, unowned

    reference count 에 κ΄€μ—¬ν•˜μ§€ μ•ŠλŠ”λ‹€.

    주둜 reference cycle 을 λ°©μ§€ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€.

    object life time 의 νŠΉμ„± λ•Œλ¬Έμ—, μ‚¬μš©μ— 따라 potential bugs λ₯Ό 포함할 수 μžˆλ‹€.

    weak ν˜Ήμ€ unowned 의 μ‚¬μš© μ΄μœ κ°€ 단지 reference cycle 방지 λͺ©μ μ΄λΌλ©΄, λŒ€μ•ˆμ„ μ°Ύμ•„μ„œ μˆ˜μ •ν•  수 μžˆλ‹€.

    μ˜ˆμ‹œ) 사이클 관계λ₯Ό 트리 κ΄€κ³„λ‘œ λ³€κ²½

     

    μ˜μƒ 전체 정리 λ³΄λŸ¬κ°€κΈ° πŸ‘ˆπŸ»

     


    μ˜€λŠ˜λ„ λ‚˜λŠ” μ„±μž₯ν–ˆλ‹€!!πŸ”₯

     

    πŸ€– [Back to the Basics] πŸ’»
    μ£Όλ‹ˆμ–΄ iOS 개발자 우짱의 기술 λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.

     

    [μ°Έκ³  자료 πŸ™‡πŸ»‍♂️]

    https://developer.apple.com/videos/play/wwdc2021/10216/

     

    'iOS > πŸ‘¨πŸ»β€πŸ’» WWDC 와 ν…Œν¬μ„Έλ―Έλ‚˜' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

    [WWDC 20] Embrace Swift Type Inference  (0) 2022.05.23

    λŒ“κΈ€

μ–΄μ œλ³΄λ‹€ λ°œμ „ν•œ λ‚˜