Publishers.Output

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋ฐœํ–‰๋œ ์š”์†Œ์˜ ์‹œํ€€์Šค ๋‚ด์—์„œ ๋ฒ”์œ„๋กœ ์ง€์ •๋œ ์š”์†Œ๋“ค์„ ๋ฐœํ–‰ํ•˜๋Š” Publisher

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

  • upstream : ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher

  • range : ๋ฐœํ–‰ํ•  ์š”์†Œ๊ฐ€ ์†ํ•œ ๋ฒ”์œ„

output ์˜คํผ๋ ˆ์ดํ„ฐ์™€ ๊ด€๋ จ์ด ์žˆ๋‹ค.

// 1 : Publishers.Output Publisher
Publishers
  .Output(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3]), range: 0 ..< 2)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Output Error")
    case .finished:
      print("Combine Output Finish")
    }
  }, receiveValue: { value in
    print("Combine Output : \(value)")
  })
  .store(in: &cancellables)

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

// 2 : prefix Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
  .prefix(2)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Output Error")
    case .finished:
      print("Combine Output Finish")
    }
  }, receiveValue: { value in
    print("Combine Output : \(value)")
  })
  .store(in: &cancellables)

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

// 3 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
  .output(at: 1)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Output Error")
    case .finished:
      print("Combine Output Finish")
    }
  }, receiveValue: { value in
    print("Combine Output : \(value)")
  })
  .store(in: &cancellables)

// Combine Output : 2
// Combine Output Finish

// 4 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
  .output(in: 0 ..< 2)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Output Error")
    case .finished:
      print("Combine Output Finish")
    }
  }, receiveValue: { value in
    print("Combine Output : \(value)")
  })
  .store(in: &cancellables)

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

๊ฐ ์ฝ”๋“œ์˜ ์ƒ์œ„ Publisher๋Š” 1, 2, 3์˜ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‚ธ๋‹ค.

1์˜ ์ฝ”๋“œ๋Š” 0 ..< 2์˜ ๋ฒ”์œ„์— ์˜ํ•ด ํ•ด๋‹น ๋ฒ”์œ„์— ์žˆ๋Š” 1, 2์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

2์˜ ์ฝ”๋“œ๋Š” prefix(2) ์— ์˜ํ•ด ์‚ฌ์šฉํ•˜์—ฌ ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๋ฐœํ–‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 1, 2์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

3์˜ ์ฝ”๋“œ๋Š” output(at: 1) ์— ์˜ํ•ด ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” 2์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

4์˜ ์ฝ”๋“œ๋Š” output(in: 0 ..< 2) ์— ์˜ํ•ด ๋ฒ”์œ„์— ์žˆ๋Š” 1, 2์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

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

Observable.from([1, 2, 3])
  .elementAt(1)
  .subscribe(onNext: { value in
    print("RxSwift Output : \(value)")
  }, onError: { _ in
    print("RxSwift Output Error")
  }, onCompleted: {
    print("RxSwift Output Finish")
  })
  .disposed(by: disposeBag)

// RxSwift Output : 2
// RxSwift Output Finish

ReactiveSwift

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

์ฐธ๊ณ 

ReactiveX - Operators - ElementAt

Last updated

Was this helpful?