Publishers.Last

μ œλ„€λ¦­ ꡬ쑰체 | 슀트림이 μ’…λ£Œν•œ ν›„ 슀트림의 λ§ˆμ§€λ§‰ μš”μ†Œλ§Œμ„ λ°œν–‰ν•˜λŠ” Publisher

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

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

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

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

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

// Combine Last : 5
// Combine Last Finish

μƒμœ„ PublisherλŠ” 1, 2, 3, 4, 5의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚Έλ‹€.

last에 μ˜ν•΄ λ§ˆμ§€λ§‰ μš”μ†Œλ§Œ λ°œν–‰λ˜λ©°, 결과적으둜 5의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

takeLast μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

ReactiveSwift

last μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

μ°Έκ³ 

ReactiveX - Operators - Last

Last updated

Was this helpful?