Publishers.Drop

μ œλ„€λ¦­ ꡬ쑰체 | λ‚˜μ€‘μ— μš”μ†Œκ°€ λ‹€μ‹œ λ°œν–‰λ˜κΈ° 이전에 λͺ…μ‹œν•œ 개수의 μš”μ†Œλ₯Ό μƒλž΅ν•˜λŠ” Publisher

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

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

  • count : μƒλž΅ν•  μš”μ†Œμ˜ 개수

쑰건 없이 μ•žμ—μ„œ λͺ‡ 개의 μš”μ†Œλ₯Ό μƒλž΅ν•˜κ³  싢을 λ•Œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

μ΄ˆκΈ°κ°’μ„ κ°–λŠ” CurrentValueSubjectλ₯Ό μ‚¬μš©ν•˜λŠ” 경우, μ΄ˆκΈ°κ°’μ„ λ¬΄μ‹œν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•  수 μžˆλ‹€.

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

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

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

// Combine Drop : 3
// Combine Drop Finish

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

2개의 μš”μ†Œλ₯Ό μƒλž΅ν•˜κ²Œ ν•˜μ˜€μœΌλ―€λ‘œ μ΅œμ’…μ μœΌλ‘œ 3의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

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

Observable.from([1, 2, 3])
  .skip(2)
  .subscribe(onNext: { value in
    print("RxSwift Drop : \(value)")
  }, onError: { _ in
    print("RxSwift Drop Error")
  }, onCompleted: {
    print("RxSwift Drop Finish")
  })
  .disposed(by: disposeBag)

// RxSwift Drop : 3
// RxSwift Drop Finish

ReactiveSwift

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

SignalProducer([1, 2, 3])
  .skip(first: 2)
  .start { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Drop : \(value)")
    case .failed:
      print("ReactiveSwift Drop Error")
    case .completed:
      print("ReactiveSwift Drop Finish")
    default:
      break
    }
  }

// ReactiveSwift Drop : 3
// ReactiveSwift Drop Finish

μ°Έκ³ 

ReactiveX - Operators - Skip

Last updated