Publishers.Zip

μ œλ„€λ¦­ ꡬ쑰체 | 두 개의 μƒμœ„ Publisher에 zip ν•¨μˆ˜λ₯Ό μ μš©ν•˜μ—¬ μƒμ„±λ˜λŠ” Publisher

두 개의 슀트림의 배좜 ν•­λͺ©μ΄ μŒμ„ 이룰 λ•Œ κ·Έ 값듀에 λŒ€ν•˜μ—¬ ν•¨μˆ˜λ₯Ό μ μš©ν•œ 값을 λ°œν–‰ν•œλ‹€.

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” μ‘°ν•©ν•  두 개의 Publisherλ₯Ό λ°›λŠ”λ‹€.

μΈμžμ— λ“€μ–΄κ°€λŠ” λͺ¨λ“  Publisherd의 μ—λŸ¬ νƒ€μž…μ€ κ°™μ•„μ•Ό ν•œλ‹€.

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

let aSubject = PassthroughSubject<Int, Never>()
let bSubject = PassthroughSubject<String, Never>()

// 1. Publishers.Zip Publisher
Publishers
  .Zip(aSubject, bSubject)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Zip Error")
    case .finished:
      print("Combine Zip Finish")
    }
  }, receiveValue: { value in
    print("Combine Zip : \(value)")
  })
  .store(in: &cancellables)

// Combine Zip : (1, "a")
// Combine Zip : (2, "b")

// 2. zip Operator
aSubject
  .zip(bSubject)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Zip Error")
    case .finished:
      print("Combine Zip Finish")
    }
  }, receiveValue: { value in
    print("Combine Zip : \(value)")
  })
  .store(in: &cancellables)

// Combine Zip : (1, "a")
// Combine Zip : (2, "b")

// 3. zip Operator
aSubject
  .zip(bSubject) { "\($0)\($1)" }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Zip Error")
    case .finished:
      print("Combine Zip Finish")
    }
  }, receiveValue: { value in
    print("Combine Zip : \(value)")
  })
  .store(in: &cancellables)

// Combine Zip : 1a
// Combine Zip : 2b

// 4
aSubject.send(1)
// 5
aSubject.send(2)
// 6
bSubject.send("a")
// 7
bSubject.send("b")
// 8
aSubject.send(3)

Int νƒ€μž… 값을 λ°›λŠ” aSubject와 String νƒ€μž… 값을 λ°›λŠ” bSubjectλ₯Ό zip μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ²°ν•©ν•˜μ˜€λ‹€.

1κ³Ό 2의 μ½”λ“œλŠ” λ‹€μŒκ³Ό 같이 λ™μž‘ν•œλ‹€.

  1. 4의 싀행에 μ˜ν•΄ aSubject에 1의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

  2. 5의 싀행에 μ˜ν•΄ aSubject에 2의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

  3. 6의 싀행에 μ˜ν•΄ bSubject에 a의 값을 μ „λ‹¬ν•œλ‹€. aSubjectμ—λŠ” 1κ³Ό 2의 값이 μ „λ‹¬λ˜μ—ˆκ³ , 1의 값이 a의 κ°’κ³Ό μŒμ„ 이루어 ν•΄λ‹Ή PublisherλŠ” (1, "a")의 값을 λ‚Έλ‹€.

  4. 7의 싀행에 μ˜ν•΄ bSubject에 b의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” 1κ³Ό 2의 값이 μ „λ‹¬λ˜μ—ˆκ³ , 2의 값이 b의 κ°’κ³Ό μŒμ„ 이루어 ν•΄λ‹Ή PublisherλŠ” (2, "b")의 값을 λ‚Έλ‹€.

  5. 8의 싀행에 μ˜ν•΄ aSubject에 3의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

3의 μ½”λ“œλŠ” λ‹€μŒκ³Ό 같이 λ™μž‘ν•œλ‹€.

  1. 4의 싀행에 μ˜ν•΄ aSubject에 1의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

  2. 5의 싀행에 μ˜ν•΄ aSubject에 2의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

  3. 6의 싀행에 μ˜ν•΄ bSubject에 a의 값을 μ „λ‹¬ν•œλ‹€. aSubjectμ—λŠ” 1κ³Ό 2의 값이 μ „λ‹¬λ˜μ—ˆκ³ , 1의 값이 a의 κ°’κ³Ό μŒμ„ 이루어 ν•΄λ‹Ή PublisherλŠ” 1a의 값을 λ‚Έλ‹€.

  4. 7의 싀행에 μ˜ν•΄ bSubject에 b의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” 1κ³Ό 2의 값이 μ „λ‹¬λ˜μ—ˆκ³ , 2의 값이 b의 κ°’κ³Ό μŒμ„ 이루어 ν•΄λ‹Ή PublisherλŠ” 2b의 값을 λ‚Έλ‹€.

  5. 8의 싀행에 μ˜ν•΄ aSubject에 3의 값을 μ „λ‹¬ν•œλ‹€. bSubjectμ—λŠ” ν•΄λ‹Ή κ°’κ³Ό μŒμ„ 이룰 수 μžˆλŠ” 값이 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή PublisherλŠ” 값을 내지 μ•ŠλŠ”λ‹€.

RxSwift

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

let aSubject = PublishSubject<Int>()
let bSubject = PublishSubject<String>()

Observable.zip(aSubject, bSubject)
  .subscribe(onNext: { value in
    print("RxSwift Zip : \(value)")
  }, onError: { _ in
    print("RxSwift Zip Error")
  }, onCompleted: {
    print("RxSwift Zip Finish")
  })
  .disposed(by: disposeBag)

aSubject.onNext(1)
aSubject.onNext(2)
bSubject.onNext("a")
bSubject.onNext("b")
aSubject.onNext(3)

// RxSwift Zip : (1, "a")
// RxSwift Zip : (2, "b")

ReactiveSwift

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

let aProperty = MutableProperty<Int>(0)
let bProperty = MutableProperty<String>("")

aProperty.signal
  .zip(with: bProperty.signal)
  .observe { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Zip : \(value)")
    case .failed:
      print("ReactiveSwift Zip Error")
    case .completed:
      print("ReactiveSwift Zip Finish")
    default:
      break
    }
  }

aProperty.value = 1
aProperty.value = 2
bProperty.value = "a"
bProperty.value = "b"
aProperty.value = 3

// ReactiveSwift Zip : (1, "a")
// ReactiveSwift Zip : (2, "b")
// ReactiveSwift Zip Finish

μ°Έκ³ 

ReactiveX - Operators - Zip

Last updated