์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์คํจํ ์์ Publisher์ ๋ํ์ฌ ๊ตฌ๋
์ ๋ค์ ์์ฑํ๋ ๊ฒ์ ์๋ํ๋ Publisher
์ด๋์
๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisher
retries
: ์ต๋ ์ฌ์๋ ํ์. nil์ด ๋ค์ด๊ฐ๋ฉด ์์ Publisher๊ฐ ์๋ฌ๋ฅผ ๋ด์ง ์์ ๋๊น์ง ์ฌ์๋ํ๋ค.
์๋ฅผ ๋ค์ด ํน์ ๋คํธ์ํฌ ์์ฒญ์ด ์ฑ๊ณตํ ๋๊น์ง ๋ค์ ์์ฒญํ๋ ค ํ๋ค๋ฉด ์ด ๋์์ ์ฌ์ฉํ ์ ์๋ค.
retry
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// Publishers.Retry Publisher
Publishers.Retry(upstream: Fail<Void, Error>(error: error), retries: 2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Retry Error")
case .finished:
print("Combine Retry Finish")
}
}, receiveValue: { value in
print("Combine Retry : \(value)")
})
.store(in: &cancellables)
// retry Operator
Fail<Void, Error>(error: error)
.retry(2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Retry Error")
case .finished:
print("Combine Retry Finish")
}
}, receiveValue: { value in
print("Combine Retry : \(value)")
})
.store(in: &cancellables)
// Combine Retry Error
์์ ์ฝ๋๋ ์์ Publisher๋ก Fail
Publisher๋ฅผ ๋ช
์ํ์๋ค.
๊ทธ๋ฌ๋ฏ๋ก ์ฒซ ๊ตฌ๋
์์ ์๋ฌ๊ฐ ๋ด๋ ค์ค๊ฒ ๋๊ณ , ์ดํ ๋ ๋ฒ ์ฌ์๋ํ๋ค.
๋ ๋ฒ์ ์ฌ์๋์๋ ๋ถ๊ตฌํ๊ณ ์๋ฌ๊ฐ ๋ด๋ ค์ค๋ฏ๋ก ๊ฒฐ๊ณผ์ ์ผ๋ก ์๋ฌ๋ฅผ ๋ธ๋ค.
RxSwift
retry
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Observable<Void>.error(error)
.retry(2)
.subscribe(onNext: { value in
print("RxSwift Retry : \(value)")
}, onError: { _ in
print("RxSwift Retry Error")
}, onCompleted: {
print("RxSwift Retry Finish")
})
.disposed(by: disposeBag)
// RxSwift Retry Error
ReactiveSwift
retry
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
SignalProducer<Void, Error>(error: error)
.retry(upTo: 2)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Retry : \(value)")
case .failed:
print("ReactiveSwift Retry Error")
case .completed:
print("ReactiveSwift Retry Finish")
default:
break
}
}
// ReactiveSwift Retry Error
์ฐธ๊ณ
ReactiveX - Operators - Retry