Publishers.ContainsWhere

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

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

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

  • predicate : ์š”์†Œ๊ฐ€ ์กฐ๊ฑด๊ณผ ๋งž๋Š”์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ ํด๋กœ์ €

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

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

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

// contains Operator
Publishers.Sequence<[Int], Never>(sequence: [2, 4, 6])
  .contains { $0 == 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 ContainsWhere : false
// Combine ContainsWhere Finish

ํด๋กœ์ €๋กœ ์กฐ๊ฑด ํ‰๊ฐ€์‹์„ ๋„˜๊ฒจ ํ•ด๋‹น ์กฐ๊ฑด์„ ํ†ต๊ณผํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐœํ–‰ํ•œ๋‹ค.

์ƒ์œ„ Publisher๋Š” 2, 4, 6์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ๋‚ด๋ฉฐ, ์กฐ๊ฑด์ธ $0 == 5๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†์œผ๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ false๋ฅผ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

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

ReactiveSwift

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

์ฐธ๊ณ 

ReactiveX - Operators - Contains

Last updated