반응형
Combine으로 Timer를 사용하는 방법은 다음과 같습니다.
- Timer
import Combine
var timerCancellable: Cancellable?
// 1초마다 수행
timerCancellable = Timer.publish(every: 1, on: .main, in: .common).autoconnect().sink { [weak self] _ in
...
}
// 타이머 취소하기
timerCancellable?.cancel()
- RunLoop
import Combine
var timerCancellable: Cancellable?
// 1초 마다 수행
let mainRunLoop = RunLoop.main
timerCancellable = mainRunLoop.schedule(after: mainRunLoop.now, interval: .seconds(1), tolerance: .milliseconds(0)) {
...
}
// 타이머 취소하기
timerCancellable?.cancel()
- DispatchQueue
import Combine
var timerCancellable: Cancellable?
let mainQueue = DispatchQueue.main
// 1초마다 수행
timerCancellable = mainQueue.schedule(after: mainQueue.now, interval: .seconds(1)) {
...
}
// 타이머 취소하기
timerCancellable?.cancel()
반응형
'iOS > Combine' 카테고리의 다른 글
PassthroughSubject vs CurrentValueSubject (1) | 2023.12.01 |
---|---|
Throttle과 Debounce의 차이점 (0) | 2023.05.31 |
PassthroughSubject (0) | 2023.05.31 |
사용자 정의(custom) 구독자(subscriber) 만들기 (0) | 2023.05.10 |
Combine과 Property Wrapper를 이용해서 UserDefault 쉽게 사용하기 (0) | 2023.05.08 |
UIKit(UIControl) + Combine을 위한 Extension (0) | 2022.11.09 |
MVVM with Combine Tutorial for iOS (1) | 2019.09.05 |
RxSwift 와 Combine (0) | 2019.07.26 |