iOS developer essential skills/Modern Concurency
Modern Concurrency Deep Dive(8)
jgkim1008
2024. 11. 21. 22:31
TaskGroup
await withTaskGroup(of: UIImage?.self, returning: [UIImage].self) { group in
group.addTask {
// Do SomeThing // ex: 2번 쓰레드 작업됨
}
group.addTask {
// Do SomeThing g // ex: 3번 쓰레드 작업됨
}
for await image in group { // 비동기 반복문이며 ex: 4번 쓰레드 작업됨,Race Condition 해결해줌
if let image = image {
imageArray.append(image)
}
}
}
- groupTask중 먼저 끝나는 순서대로 imageArrayAppend 수행
- group은 내부적으로 task가 끝나는 시점을 알고 있다.
- 부모를 생성하여 task를 관리한다(TaskGroup)