iOS developer essential skills/Modern Concurency
Modern Concurrency Deep Dive(6)
jgkim1008
2024. 11. 19. 23:27
Delegate with Continuation
- continuation을 변수로 설정한후, Delegate에서 continuation을 resume하면 비동기를 실행시킬수 있다.
- refactor -> addAsyncWrapper 설정하면 콜백방식에서 continuation으로 자동으로 바꿔준다.
- refactor -> alternative 콜백방식으로 사용할수 있게 해준다.
final class MockModule: NSObject, CLLocationManagerDelegate {
private var continuation: LCContinuation?
func loactionManager(didUpdateLocations locations: [CLLocation]) {
continuation?.resume(returning: location)
continuation = nil
}
}
final class MockViewModel: ObservableObject {
do {
let location: CLLocation = try await withCheckedThrowingContinuation { [weak self] contination in
self?.locationService.start()
await MainActor.run {
// UI 업데이트
}
} catch {
}
}