โœจ
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.Catch

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์‹คํŒจํ•˜๋Š” Publisher๋ฅผ ๋‹ค๋ฅธ Publisher๋กœ ๊ต์ฒดํ•˜์—ฌ ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher๋กœ๋ถ€ํ„ฐ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Publisher

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

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

  • handler : ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š” ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด Publisher๋ฅผ ๋งŒ๋“œ๋Š” ํด๋กœ์ €

์ƒ์œ„์— ํ๋ฅด๋Š” Publisher์™€ ์ƒˆ๋กœ์šด Publisher์˜ Output ํƒ€์ž…์€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

์ƒ์œ„ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ๋‚ด๋ฉฐ ์ข…๋ฃŒํ•˜๋Š” ๋Œ€์‹  ๋‹ค๋ฅธ Publisher๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

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

// Publishers.Catch Publisher
Publishers.Catch(upstream: Fail(error: error)) { _ in Just("Error") }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Catch Error")
    case .finished:
      print("Combine Catch Finish")
    }
  }, receiveValue: {
    print("Combine Catch : \($0)")
  })
  .store(in: &cancellables)

// catch Operator
Fail(error: error)
  .catch { _ in Just("Error") }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Catch Error")
    case .finished:
      print("Combine Catch Finish")
    }
  }, receiveValue: {
    print("Combine Catch : \($0)")
  })
  .store(in: &cancellables)

// Combine Catch : Error
// Combine Catch Finish

์œ„์˜ ์ฝ”๋“œ๋Š” ์‹คํŒจํ•˜๋Š” Publisher์ธ Fail๋กœ๋ถ€ํ„ฐ catch ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—๋Ÿฌ๋ฅผ ๋‚ด๊ณ  ์ข…๋ฃŒํ•˜๋Š” ๋Œ€์‹  Just Publisher๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

RxSwift

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

Observable.error(error)
  .catchError { _ in Observable.just("Error") }
  .subscribe(onNext: {
    print("RxSwift Catch : \($0)")
  }, onError: { _ in
    print("RxSwift Catch Error")
  }, onCompleted: {
    print("RxSwift Catch Finish")
  })
  .disposed(by: disposeBag)

// RxSwift Catch : Error
// RxSwift Catch Finish

ReactiveSwift

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

SignalProducer(error: error)
  .flatMapError { error in SignalProducer(value: "Error") }
  .start { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Catch : \(value)")
    case .failed:
      print("ReactiveSwift Catch Error")
    case .completed:
      print("ReactiveSwift Catch Finish")
    default:
      break
    }
  }

// ReactiveSwift Catch : Error
// ReactiveSwift Catch Finish

์ฐธ๊ณ 

PreviousPublishers.EncodeNextPublishers.ReplaceError

Last updated 5 years ago

Was this helpful?

ReactiveX - Operators - Catch