Publishers.Count

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์š”์†Œ๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” Publisher

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

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

์ƒ์œ„ Publisher๊ฐ€ ๋ฐœํ–‰ํ•˜๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐฐ์ถœํ•œ๋‹ค.

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

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

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

// Combine Count : 3
// Combine Count Finish

์ƒ์œ„ Publisher์— ํ๋ฅด๋Š” ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ 3์ด๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ 3์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

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

ReactiveSwift

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

์ฐธ๊ณ 

ReactiveX - Operators - Count

Last updated

Was this helpful?