μ λ€λ¦ ꡬ쑰체 | μλμ νλ₯΄λ 리μλ²μκ² μμμ μλ£μ μ λ¬μ μ§μ°μν€λ Publisher
μ΄λμ
λΌμ΄μ λ λ€ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
interval
: μ§μ°μν¬ μκ°
tolerance
: μ΄λ°νλ μ§μ°λ μ΄λ²€νΈμμ νμ©λ λ΄μ±
μ΄ νλ‘νΌν°λ₯Ό μ€μ λ μκ°λ§νΌ μμ€ν
μ μ΄λ²€νΈμ λ°°μΆμ μ¬μ λ‘κ² ν μ μλ€. μ μ°νκ²(κ°μ΄ ν¬κ²) μ€μ λ μλ‘ μμ€ν
μ μ λ ₯ μλΉλ₯Ό μ€μ΄κ³ μλ΅μ±μ μ’κ² ν μ μλ€.
scheduler
: μ§μ°λ μ΄λ²€νΈλ₯Ό μ λ¬νλ μ€μΌμ€λ¬
options
: μ€μΌμ€λ¬ μ΅μ
μ£Όμ΄μ§ μκ°λ§νΌ μ€νΈλ¦ΌμΌλ‘λΆν°μ κ° λ°°μΆμ 미룬λ€.
delay
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.Delay Publisher
Publishers.Delay(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3]), interval: .seconds(1), tolerance: .zero, scheduler: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Delay Error")
case .finished:
print("Combine Delay Finish")
}
}, receiveValue: { value in
print("Combine Delay : \(value)")
})
.store(in: &cancellables)
// delay Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.delay(for: .seconds(1), scheduler: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Delay Error")
case .finished:
print("Combine Delay Finish")
}
}, receiveValue: { value in
print("Combine Delay : \(value)")
})
.store(in: &cancellables)
// μΆλ ₯μ 1μ΄ νμ λ°μνλ€.
// Combine Delay : 1
// Combine Delay : 2
// Combine Delay : 3
// Combine Delay Finish
μμ Publisherλ 1, 2, 3μ κ°μ μ°¨λ‘λλ‘ λΈλ€.
1μ΄ μ§μ°νμ¬ μλλ‘ λ³΄λ΄λ―λ‘ 1μ΄ νμ μ΄λ²€νΈκ° λ°μνλ€.
RxSwift
delay
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.from([1, 2, 3])
.delay(.seconds(1), scheduler: MainScheduler.instance)
.subscribe(onNext: { value in
print("RxSwift Delay : \(value)")
}, onError: { _ in
print("RxSwift Delay Error")
}, onCompleted: {
print("RxSwift Delay Finish")
})
.disposed(by: disposeBag)
// μΆλ ₯μ 1μ΄ νμ λ°μνλ€.
// RxSwift Delay : 1
// RxSwift Delay : 2
// RxSwift Delay : 3
// RxSwift Delay Finish
ReactiveSwift
delay
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer([1, 2, 3])
.delay(1, on: QueueScheduler.main)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Delay : \(value)")
case .failed:
print("ReactiveSwift Delay Error")
case .completed:
print("ReactiveSwift Delay Finish")
default:
break
}
}
// μΆλ ₯μ 1μ΄ νμ λ°μνλ€.
// ReactiveSwift Delay : 1
// ReactiveSwift Delay : 2
// ReactiveSwift Delay : 3
// ReactiveSwift Delay Finish
μ°Έκ³
ReactiveX - Operators - Delay