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