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