Publishers.AllSatisfy

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ „๋‹ฌ๋ฐ›์€ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ํ†ต๊ณผํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐœํ–‰ํ•˜๋Š” Publisher

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

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

  • predicate : ๊ฐ๊ฐ์˜ ์ „๋‹ฌ๋ฐ›์€ ์š”์†Œ๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ํด๋กœ์ €

์ƒ์œ„ Publisher๊ฐ€ ๋‚ด๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ํ†ต๊ณผํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ชจ๋“  ๊ฐ’๋“ค์ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

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

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

// Combine AllSatisfy : false
// Combine AllSatisfy Finish

์ƒ์œ„ Publisher๊ฐ€ 2, 4, 7์˜ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‚ด๊ณ , ์กฐ๊ฑด์œผ๋กœ ๊ฐ’์ด 2์˜ ๋ฐฐ์ˆ˜์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ฒŒ ํ•˜์˜€๋‹ค.

2, 4์˜ ๊ฐ’์€ 2์˜ ๋ฐฐ์ˆ˜์ด๋‚˜ 7์˜ ๊ฐ’์€ 2์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ชจ๋“  ๊ฐ’๋“ค์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

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

ReactiveSwift

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

์ฐธ๊ณ 

ReactiveX - Operators - All

Last updated

Was this helpful?