Publishers.DropWhile

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ฃผ์–ด์ง„ ํด๋กœ์ €๊ฐ€ false๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ๊นŒ์ง€ ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher๋กœ๋ถ€ํ„ฐ ์š”์†Œ๋ฅผ ์ƒ๋žตํ•˜๋Š” Publisher

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

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

  • predicate : ์š”์†Œ์˜ ์ƒ๋žต ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ํด๋กœ์ €

์กฐ๊ฑด์— ๋งž์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€๋Š” ๋ฐœํ–‰๋œ ๊ฐ’์„ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

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

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

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

// Combine DropWhile : 3
// Combine DropWhile Finish

์ƒ์œ„ Publisher๊ฐ€ 1, 2, 3์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ๋‚ธ๋‹ค.

3 ๋ฏธ๋งŒ์ผ ๋•Œ๊นŒ์ง€ ๋ฐœํ–‰๋œ ๊ฐ’์„ ์ƒ๋žตํ•˜๋ผ๋Š” ์กฐ๊ฑด์— ์˜ํ•ด 3์˜ ๊ฐ’์„ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

skipWhile ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

// RxSwift DropWhile : 3
// RxSwift DropWhile Finish

ReactiveSwift

skip ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

// ReactiveSwift DropWhile : 3
// ReactiveSwift DropWhile Finish

์ฐธ๊ณ 

ReactiveX - Operators - Skip

Last updated

Was this helpful?