μ λ€λ¦ ꡬ쑰체 | νΉμ μ€μΌμ€λ¬μμ μμμ νλ₯΄λ Publisherλ‘λΆν° μμλ₯Ό λ°λ Publisher
μ΄λμ
λΌμ΄μ λ μΈ κ°μ μΈμλ₯Ό μꡬνλ€.
upstream
: μμμ νλ₯΄λ Publisher
scheduler
: μ΄μ 체μΈμ΄ λμν μ€μΌμ€λ¬
options
: μ€μΌμ€λ¬μ μ΅μ
.
RunLoop
μ κ²½μ° μ€μΌμ€λ¬ μ΅μ
μ μ§μνμ§ μλλ€.
DispatchQueue
μ κ²½μ° DispatchQoS
, DispatchWorkItemFlags
, DispatchGroup
λ±μ μ μνμ¬ μ€μΌμ€λ¬ μ΅μ
μ μ§μ ν μ μλ€.
μ΄ μ½λλ₯Ό μμ±νκΈ° μ μ 체μΈμ λͺ
μν μ€μΌμ€λ¬μμ λμνκ² λλ€.
Combineμ Scheduler
νλ‘ν μ½μ μ μνλ©°, OperationQueue
, DispatchQueue
, RunLoop
λ±μ΄ νλ‘ν μ½μ μ±ννλ€.
subscribe
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.SubscribeOn Publisher
Publishers.SubscribeOn(upstream: Just(Void()), scheduler: DispatchQueue.main, options: nil)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine SubscribeOn Error")
case .finished:
print("Combine SubscribeOn Finish")
}
}, receiveValue: {
print("Combine SubscribeOn")
})
.store(in: &cancellables)
// subscribe Operator
Just(Void())
.subscribe(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine SubscribeOn Error")
case .finished:
print("Combine SubscribeOn Finish")
}
}, receiveValue: {
print("Combine SubscribeOn")
})
.store(in: &cancellables)
// Combine SubscribeOn
// Combine SubscribeOn Finish
DispatchQueue.main
μμ λμνλλ‘ μ€μΌμ€λ¬λ₯Ό λͺ
μν΄ μ£ΌμμΌλ―λ‘ ν΄λΉ μ½λκ° λͺ
μλκΈ° μ΄μ μ λμμ λ©μΈ μ€λ λμμ μ΄λ£¨μ΄μ§κ² λλ€.
RxSwift
subscribeOn
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.just(Void())
.subscribeOn(MainScheduler.instance)
.subscribe(onNext: {
print("RxSwift SubscribeOn")
}, onError: { _ in
print("RxSwift SubscribeOn Error")
}, onCompleted: {
print("RxSwift SubscribeOn Finish")
})
.disposed(by: disposeBag)
ReactiveSwift
start
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer(value: Void())
.start(on: UIScheduler())
.start { event in
switch event {
case .value:
print("ReactiveSwift SubscribeOn")
case .failed:
print("ReactiveSwift SubscribeOn Error")
case .completed:
print("ReactiveSwift SubscribeOn Finish")
default:
break
}
}
μ°Έκ³
ReactiveX - Operators - SubscribeOn