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 }
                })
반응형