✨
Combine 🀝 RxSwift 🀝 ReactiveSwift
  • Combine 🀝 RxSwift 🀝 ReactiveSwift
  • Publisher
    • Publishers.Merge
    • Publishers.Zip3
    • Publishers.TryScan
    • Publishers.MergeMany
    • Publishers.Breakpoint
    • AnyPublisher
    • Publishers.Concatenate
    • Publishers.Drop
    • Publishers.Decode
    • Publishers.TryCompactMap
    • Publishers.CombineLatest4
    • Optional.Publisher
    • Publishers.CollectByCount
    • Publishers.TryFilter
    • Publishers.CombineLatest3
    • Publishers.TryContainsWhere
    • Just
    • Publishers.TryFirstWhere
    • Publishers.MapError
    • Publishers.RemoveDuplicates
    • Result.Publisher
    • Publishers.LastWhere
    • Publishers.First
    • Publishers.Throttle
    • Publishers.DropUntilOutput
    • Publishers.Count
    • Publishers.Comparison
    • Publishers.Debounce
    • Publishers.MapKeyPath
    • Publishers.CollectByTime
    • Publishers.TryDropWhile
    • Empty
    • Publishers.PrefixUntilOutput
    • Publishers.Print
    • URLSession.DataTaskPublisher
    • Publishers.Reduce
    • Publishers.MapKeyPath3
    • Publishers.Collect
    • Publishers.TryPrefixWhile
    • Publishers.MapKeyPath2
    • Future
    • Publishers.SubscribeOn
    • Publishers.AllSatisfy
    • Publishers.Retry
    • Publishers.ContainsWhere
    • Publishers.SetFailureType
    • Publishers.TryMap
    • Publishers.Output
    • Publishers.TryRemoveDuplicates
    • Publishers.HandleEvents
    • Publishers.Sequence
    • Publishers.TryCatch
    • Publishers.Merge8
    • Publishers.CombineLatest
    • Deferred
    • Publishers.Merge7
    • Publishers.Contains
    • Fail
    • Publishers.Merge6
    • Publishers.TryComparison
    • Publishers.Delay
    • Publishers.Scan
    • Publishers.Merge5
    • Publishers.MeasureInterval
    • Publishers.AssertNoFailure
    • Publishers.ReceiveOn
    • Publishers.Merge4
    • Publishers.DropWhile
    • Publishers.Merge3
    • Publishers.TryReduce
    • Publishers.Zip
    • Publishers.Timeout
    • Publishers.CompactMap
    • Publishers.ReplaceEmpty
    • Publishers.PrefixWhile
    • Publishers.Filter
    • Publishers.TryAllSatisfy
    • Publishers.FirstWhere
    • Publishers.Last
    • Publishers.Map
    • Publishers.SwitchToLatest
    • Publishers.TryLastWhere
    • Publishers.Encode
    • Publishers.Catch
    • Publishers.ReplaceError
    • Pulishers.IgnoreOutput
    • Publishers.Zip4
  • Subject
    • PassthroughSubject
    • CurrentValueSubject
  • Subscriber
Powered by GitBook
On this page
  • RxSwift
  • ReactiveSwift
  • μ°Έκ³ 

Was this helpful?

  1. Publisher

Publishers.Scan

μ œλ„€λ¦­ ꡬ쑰체

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

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

  • initialResult : 초기 κ°’.

  • nextPartialResult : μš”μ†Œμ— μ—°μ†μ μœΌλ‘œ μ μš©ν•  ν΄λ‘œμ €

Publisherκ°€ λ‚΄λŠ” μš”μ†Œμ— μ—°μ†μ μœΌλ‘œ ν•¨μˆ˜λ₯Ό μ μš©ν•˜κ³  μ‹€ν–‰ν•œ ν›„, μ„±κ³΅μ μœΌλ‘œ μ‹€ν–‰λœ ν•¨μˆ˜μ˜ λ°˜ν™˜ 값을 λ‚Έλ‹€.

Swift의 reduce ν•¨μˆ˜μ™€ λ™μž‘μ΄ λΉ„μŠ·ν•˜μ§€λ§Œ reduceκ°€ μ΅œμ’… κ²°κ³Όλ§Œμ„ λ°˜ν™˜ν•œλ‹€λ©΄ scan은 ν•¨μˆ˜κ°€ 적용된 μš”μ†Œμ™€ λ‹€μŒ μš”μ†Œμ— λŒ€ν•΄ ν•¨μˆ˜λ₯Ό μ μš©ν•˜μ—¬ 각 μš”μ†Œκ°€ μ μ§„μ μœΌλ‘œ λ‹€μ‹œ λ°œν–‰λœλ‹€.

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

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

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

// Combine Scan : 1
// Combine Scan : 3
// Combine Scan : 6
// Combine Scan Finish

μƒμœ„ PublisherλŠ” 1, 2, 3의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚Έλ‹€. μ μ§„μ μœΌλ‘œ μ΄μ „μ˜ κ°’κ³Ό ν˜„μž¬μ˜ 값을 λ”ν•œ 값을 λ°˜ν™˜ν•˜λ„λ‘ ν•˜μ˜€κ³ , λ‹€μŒκ³Ό 같이 λ™μž‘ν•œλ‹€.

  1. 1이 μ΄ˆκΈ°κ°’ 0κ³Ό 더해져 1의 값을 λ‚Έλ‹€.

  2. 2κ°€ μ΄μ „μ˜ κ°’ 1κ³Ό 더해져 3의 값을 λ‚Έλ‹€.

  3. 3이 μ΄μ „μ˜ κ°’ 3κ³Ό 더해져 6의 값을 λ‚Έλ‹€.

결과적으둜 1, 3, 6의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

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

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

// RxSwift Scan : 1
// RxSwift Scan : 3
// RxSwift Scan : 6
// RxSwift Scan Finish

ReactiveSwift

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

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

// ReactiveSwift Scan : 1
// ReactiveSwift Scan : 3
// ReactiveSwift Scan : 6
// ReactiveSwift Scan Finish

μ°Έκ³ 

PreviousPublishers.DelayNextPublishers.Merge5

Last updated 5 years ago

Was this helpful?

ReactiveX - Operators - Scan