Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 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)
  }
}
Comments