Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 클린 코드
- ios
- tableView
- ribs
- 애니메이션
- uitableview
- rxswift
- Refactoring
- UITextView
- clean architecture
- 리펙토링
- 리팩토링
- RxCocoa
- swiftUI
- UICollectionView
- 리펙터링
- Xcode
- SWIFT
- combine
- map
- swift documentation
- HIG
- Protocol
- 스위프트
- Observable
- uiscrollview
- MVVM
- Human interface guide
- collectionview
- Clean Code
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[iOS - swift] RxSwift로 Timer 구현 방법 (Foreground, Background, interval 연산자) 본문
iOS 응용 (swift)
[iOS - swift] RxSwift로 Timer 구현 방법 (Foreground, Background, interval 연산자)
jake-kim 2021. 12. 22. 00:51* 타이머 종류
- foreground에서만 동작하는 Timer
- background에서도 동작하는듯한 Timer
Foreground에서만 동작하는 Timer
- 변수 하나를 선언해놓고 RxSwift의 연산자 interval(_:scheduler:) 사용
// ViewController.swift
private var currentCount = 0
private func setupOnlyForegroundTimer() {
let timer = Observable<Int>.interval(
.seconds(1),
scheduler: MainScheduler.instance
)
timer.withUnretained(self)
.do(onNext: {
weakSelf, countValue in
weakSelf.currentCount += 1
weakSelf.countLabel1.text = "타이머1: \(weakSelf.currentCount)초"
})
.subscribe()
.disposed(by: disposeBag)
}
Background에서도 동작하는듯한 Timer
- Timer를 처음 실행할 때의 시간을 기록해놓고, 타이머가 동작할 때 해당 값을 현재 시간에서 빼서 값 획득
private func setupPossibleBackgroundTimer() {
let startTime = Date()
let timer = Observable<Int>.interval(
.seconds(1),
scheduler: MainScheduler.instance
)
timer.withUnretained(self)
.do(onNext: { weakSelf, countValue in
let elapseSeconds = Date().timeIntervalSince(startTime)
weakSelf.countLabel2.text = "타이머2: \(Int(elapseSeconds))초"
})
.subscribe()
.disposed(by: disposeBag)
}
* 전체 소스 코드
// ViewController.swift
import UIKit
import RxSwift
import SnapKit
import Then
class ViewController: UIViewController {
private let countLabel1 = UILabel().then {
$0.textAlignment = .center
$0.textColor = .systemBlue
}
private let countLabel2 = UILabel().then {
$0.textAlignment = .center
$0.textColor = .systemBlue
}
private var currentCount = 0
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .black
self.view.addSubview(countLabel1)
self.view.addSubview(countLabel2)
self.countLabel1.snp.makeConstraints {
$0.centerX.centerY.equalToSuperview()
}
self.countLabel2.snp.makeConstraints {
$0.top.equalTo(self.countLabel1.snp.bottom).offset(16)
$0.centerX.equalToSuperview()
}
self.setupOnlyForegroundTimer()
self.setupPossibleBackgroundTimer()
}
private func setupOnlyForegroundTimer() {
let timer = Observable<Int>.interval(
.seconds(1),
scheduler: MainScheduler.instance
)
timer.withUnretained(self)
.do(onNext: {
weakSelf, countValue in
weakSelf.currentCount += 1
weakSelf.countLabel1.text = "타이머1: \(weakSelf.currentCount)초"
})
.subscribe()
.disposed(by: disposeBag)
}
private func setupPossibleBackgroundTimer() {
let startTime = Date()
let timer = Observable<Int>.interval(
.seconds(1),
scheduler: MainScheduler.instance
)
timer.withUnretained(self)
.do(onNext: { weakSelf, countValue in
let elapseSeconds = Date().timeIntervalSince(startTime)
weakSelf.countLabel2.text = "타이머2: \(Int(elapseSeconds))초"
})
.subscribe()
.disposed(by: disposeBag)
}
}
'iOS 응용 (swift)' 카테고리의 다른 글
Comments