Geon

Modern Concurrency Deep Dive(1) 본문

iOS developer essential skills/Modern Concurency

Modern Concurrency Deep Dive(1)

jgkim1008 2024. 11. 14. 23:45

Task

- 비동기의 단위이다.

- 내부에 self를 참조하고 있을때 기존의 DispatchQueue와 다르게 자동으로 self를 해제해준다.(Task closure lifetime)

- 즉 캡쳐를 해도 상관은없으나, 필요한 사항이 아니면 할필요가 없다.

 

final class Mock {
 var image: UIImage?

  func someFunction() asnyc -> UIImage?  {
     Task {
         Task.sleep(5)
         image = UIImage() // self 선언 안해줘도 된다.
      }
  }

}

 

출저: https://developer.apple.com/documentation/swift/task#Task-closure-lifetime