Publishers.Output

μ œλ„€λ¦­ ꡬ쑰체 | λ°œν–‰λœ μš”μ†Œμ˜ μ‹œν€€μŠ€ λ‚΄μ—μ„œ λ²”μœ„λ‘œ μ§€μ •λœ μš”μ†Œλ“€μ„ λ°œν–‰ν•˜λŠ” Publisher

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

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

  • range : λ°œν–‰ν•  μš”μ†Œκ°€ μ†ν•œ λ²”μœ„

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

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

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

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

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

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

// Combine Output : 2
// Combine Output Finish

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

// Combine Output : 1
// Combine Output : 2
// Combine Output Finish

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

1의 μ½”λ“œλŠ” 0 ..< 2의 λ²”μœ„μ— μ˜ν•΄ ν•΄λ‹Ή λ²”μœ„μ— μžˆλŠ” 1, 2의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

2의 μ½”λ“œλŠ” prefix(2) 에 μ˜ν•΄ μ‚¬μš©ν•˜μ—¬ μ•žμ—μ„œλΆ€ν„° 두 개의 μš”μ†Œλ₯Ό λ°œν–‰ν•œλ‹€. λ”°λΌμ„œ 1, 2의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

3의 μ½”λ“œλŠ” output(at: 1) 에 μ˜ν•΄ 첫 번째 μΈλ±μŠ€μ— μžˆλŠ” 2의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

4의 μ½”λ“œλŠ” output(in: 0 ..< 2) 에 μ˜ν•΄ λ²”μœ„μ— μžˆλŠ” 1, 2의 값을 λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

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

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

// RxSwift Output : 2
// RxSwift Output Finish

ReactiveSwift

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

μ°Έκ³ 

ReactiveX - Operators - ElementAt

Last updated