Publishers.Contains

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

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

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

  • output : ์ƒ์œ„ Publisher๋กœ๋ถ€ํ„ฐ ์Šค์บ”ํ•  ์š”์†Œ

์ƒ์œ„ Publisher์˜ Output ํƒ€์ž…์€ Equatable ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผ ํ•œ๋‹ค.

output์— ๋„˜๊ฒจ์ง„ ์š”์†Œ๊ฐ€ ์ƒ์œ„ Publisher์— ์กด์žฌํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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

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

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

// Combine Contains : false
// Combine Contains Finish

output ์ธ์ž์— ํŠน์ • ๊ฐ’์„ ๋„˜๊ฒจ ์ƒ์œ„ Publisher๊ฐ€ ๋ฐœํ–‰ํ•˜๋Š” ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ƒ์œ„ Publisher๋Š” 2, 4, 6์˜ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‚ด๊ณ , 5์˜ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

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

ReactiveSwift

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

์ฐธ๊ณ 

ReactiveX - Operators - Contains

Last updated