점진적 과부하 개발 블로그

Swift DispatchSourceTimer 본문

Swift

Swift DispatchSourceTimer

지용빡 2022. 2. 15. 22:41
반응형
Swift DispatchSourceTimer에 대해 알아보겠습니다.
GCD(Grand Central Dispatch)
  • 작업을 병렬적으로 처리하기 위해 애플에서 제공하는 API
  • 스레드를 만들거나 관리하기 어려운 일들을 맡아준다.
DispatchSourceTimer
 

Apple Developer Documentation

 

developer.apple.com

var timer: DispatchSourceTimer?

func startTimer() {
        if self.timer == nil {
            self.timer = DispatchSource.makeTimerSource(flags: [], queue: .main)
            self.timer?.schedule(deadline: .now(), repeating: 1)
            self.timer?.setEventHandler(handler: { [weak self] in
            	guard let self = self else { return }
                })
}
  • 타이머 인스턴스를 생성
self.timer = DispatchSource.makeTimerSource(flags: [], queue: .main)
  • 어떤 주기로 타이머를 실행할건지 설정
self.timer?.schedule(deadline: .now(), repeating: 1)
  • 타이머와 함께 연동될 EventHandler를 할당
self.timer?.setEventHandler(handler: { [weak self] in
            	guard let self = self else { return }
                })
반응형

'Swift' 카테고리의 다른 글

OpenWeather API 사용하는 법  (0) 2022.02.17
Swift URLSession  (0) 2022.02.16
Swift DatePicker 사용해보기  (0) 2022.02.03
Swift UICollectionView 알아보기  (0) 2022.02.02
Swift UITabBarController 알아보기  (0) 2022.02.01