관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - SwiftUI] Combine의 Scheduler (receive(on:), subscribe(on:), delay(for:scheduler:)) 사용 방법 본문

iOS Combine (SwiftUI)

[iOS - SwiftUI] Combine의 Scheduler (receive(on:), subscribe(on:), delay(for:scheduler:)) 사용 방법

jake-kim 2022. 9. 21. 22:01

목차) Combine - 목차 링크

Scheduler

https://developer.apple.com/documentation/combine/scheduler

  • 언제, 어떻게 클로저가 실행될지 정하는 프로토콜
    • thread 설정도 가능 (main, global)
    • 시간 설정도 가능

Scheduler 사용 방법

  • 스레드 변경
    • receive(on:): downstream의 스레드 변경
    • subscribe(on:): upstream의 스레드 변경
  • receive(on:), subscribe(on:) 안쓴 경우 스레드 확인
    • DispatchQueue.global()로 실행한 경우, main thread가 아닌 global thread에서 sink의 클로저 부분이 동작
    • 즉, 따로 스케줄러 설정을 하지 않으면 subject의 이벤트를 발행하는 쪽의 스케줄러와 동일하기 sink 클로저 부분이 동작
let subject = PassthroughSubject<Void, Never>()

subject
  .sink(receiveValue: { _ in print(Thread.isMainThread) })

subject.send(())

DispatchQueue.global().async {
  subject.send(())
}

/*
 true
 false
*/
  • recevie(on:) - downstream에 적용
let subject = PassthroughSubject<Void, Never>()

let cancellable = subject
  .handleEvents(receiveOutput: { print("upstream: \(Thread.isMainThread)") })
  .receive(on: DispatchQueue.main)
  .handleEvents(receiveOutput: { print("downstream: \(Thread.isMainThread)") })
  .sink(receiveValue: { _ in print()  })

DispatchQueue.global().async {
  subject.send(())
}

/*
 upstream: false
 downstream: true
*/
  • subscribe(on:) - upstream에 적용
Just(1)
  .map { _ in print(Thread.isMainThread) }
  .subscribe(on: DispatchQueue.global())
  .sink { print(Thread.isMainThread) }

/*
 true
 false
*/
  • delay를 주어, thread 변경
let cancellable = Just(1)
  .receive(on: DispatchQueue.main)
  .map { _ in print(Thread.isMainThread) }
  .delay(for: 2, scheduler: DispatchQueue.global()) // background thread로 변경
  .sink { print(Thread.isMainThread) } // 여기도 background thread 
  
/*
 true
 false
*/

* 참고

https://developer.apple.com/documentation/combine/immediatescheduler

https://www.vadimbulavin.com/understanding-schedulers-in-swift-combine-framework/

https://developer.apple.com/documentation/combine/scheduler

Comments