Publishers.LastWhere

μ œλ„€λ¦­ ꡬ쑰체 | 슀트림이 μ’…λ£Œν•˜λ©΄, νŠΉμ • 쑰건 ν΄λ‘œμ €λ₯Ό λ§Œμ‘±ν•˜κΈ° μœ„ν•œ 슀트림의 λ§ˆμ§€λ§‰ μš”μ†Œλ§Œμ„ λ°œν–‰ν•˜λŠ” Publisher

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream : μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • predicate : μ–΄λ–€ μš”μ†Œλ₯Ό λ°œν–‰ν• μ§€ κ²°μ •ν•˜λŠ” ν΄λ‘œμ €

μƒμœ„ Publisherλ‘œλΆ€ν„° 쑰건 ν΄λ‘œμ €μ— λ§Œμ‘±ν•˜λŠ” μš”μ†Œλ₯Ό λ¨Όμ € 뽑아내고, κ·Έ μ€‘μ—μ„œ λ§ˆμ§€λ§‰ μš”μ†Œλ§Œμ„ λ°œν–‰ν•œλ‹€.

last μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

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

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

// Combine LastWhere : 5
// Combine LastWhere Finish

μƒμœ„ PublisherλŠ” 1, 2, 3, 4, 5의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚΄κ³ , 2의 λ°°μˆ˜κ°€ μ•„λ‹Œ 쑰건을 μ„€μ •ν–ˆλ‹€.

κ°’λ“€ 쀑 2의 λ°°μˆ˜κ°€ μ•„λ‹Œ 1, 3, 5 쀑 λ§ˆμ§€λ§‰ μš”μ†ŒλŠ” 5μ΄λ―€λ‘œ, μ΅œμ’…μ μœΌλ‘œ 5의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

ν•΄λ‹Ή λ™μž‘μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

ReactiveSwift

ν•΄λ‹Ή λ™μž‘μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

Last updated