Deferred

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ƒˆ๋กœ์šด Subscriber๋ฅผ ์œ„ํ•œ Publisher๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณต๋œ ํด๋กœ์ €๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ๊ตฌ๋…์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” Publisher

์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ํ•œ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค.

  • createPublisher : subscribe๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‹คํ–‰ํ•  ํด๋กœ์ €. ํด๋กœ์ €๋Š” ์ง€์—ฐ ์‹คํ–‰ํ•  Publisher๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ์ง€์—ฐ ์‹คํ–‰ํ•  Publisher๋ฅผ ๋งŒ๋“œ๋Š” ํด๋กœ์ €๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ด ํด๋กœ์ €๋Š” Deferred Publisher๋ฅผ ๊ตฌ๋…ํ•  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜์–ด ๊ฐ๊ฐ์˜ ์˜ต์ €๋ฒ„๊ฐ€ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ฆผ์„ ๋ฐ›๋„๋ก ํ•œ๋‹ค.

Deferred { Just(Void()) }
  .sink(receiveValue: { print("Combine Deferred") })
  .store(in: &cancellables)

// Combine Deferred

RxSwift

deferred ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Observable.deferred { Observable.just(Void()) }
  .subscribe(onNext: { print("RxSwift Deferred") })
  .disposed(by: disposeBag)

// RxSwift Deferred

ReactiveSwift

ํ•ด๋‹น ๋™์ž‘์„ ๊ตฌํ˜„ํ•˜๋Š” ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ฐธ๊ณ 

ReactiveX - Operators - Defer

Last updated