✨
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.Timeout

μ œλ„€λ¦­ ꡬ쑰체

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

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

  • interval : νƒ€μž„μ•„μ›ƒμ„ μΌμœΌν‚¬ μ‹œκ°„

  • scheduler : μš”μ†Œλ₯Ό λ°œν–‰ν•˜λŠ” μŠ€μΌ€μ€„λŸ¬

  • options : μŠ€μΌ€μ€„λŸ¬ μ˜΅μ…˜

  • customError : νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν–ˆμ„ λ•Œ λ°œν–‰ν•  μ—λŸ¬λ₯Ό λ§Œλ“œλŠ” ν΄λ‘œμ €

μƒμœ„ Publisher에 μ£Όμ–΄μ§„ μ‹œκ°„ λ™μ•ˆ μš”μ†Œκ°€ μ „λ‹¬λ˜μ§€ μ•Šμ„ λ•Œ νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν•˜μ—¬ Publisherλ₯Ό μ’…λ£Œν•œλ‹€.

customErrorκ°€ nil이면 μ’…λ£Œν•˜λ©°, nil이 μ•„λ‹ˆλ©΄ μ—λŸ¬λ₯Ό λ‚Έλ‹€.

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

let subject = PassthroughSubject<Int, Error>()

// Publishers.Timeout (customError != nil)
Publishers.Timeout(upstream: subject, interval: .milliseconds(500), scheduler: DispatchQueue.main, options: nil) { error }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine TimeOut Error")
    case .finished:
      print("Combine TimeOut Finish")
    }
  }, receiveValue: { value in
    print("Combine TimeOut : \(value)")
  })
  .store(in: &cancellables)

// Combine Timeout Error

// timeout Operator (customError == nil)
subject
  .timeout(.milliseconds(500), scheduler: DispatchQueue.main)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine TimeOut Error")
    case .finished:
      print("Combine TimeOut Finish")
    }
  }, receiveValue: { value in
    print("Combine TimeOut : \(value)")
  })
  .store(in: &cancellables)

// Combine Timeout Finish

subject에 값을 μ „λ‹¬ν•˜λŠ” μ½”λ“œκ°€ μ—†μœΌλ―€λ‘œ, μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ 0.5초 후에 νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν•œλ‹€.

customErrorλ₯Ό μ •μ˜ν•œ 경우 νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν•  λ•Œ μ—λŸ¬λ₯Ό λ‚΄λ©°, customErrorλ₯Ό μ •μ˜ν•˜μ§€ μ•Šμ€ 경우 νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν•  λ•Œ μ’…λ£Œν•œλ‹€.

RxSwift

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

let subject = PublishSubject<Int>()

subject
  .timeout(.milliseconds(500), scheduler: MainScheduler.instance)
  .subscribe(onNext: { value in
    print("RxSwift Timeout : \(value)")
  }, onError: { _ in
    print("RxSwift Timeout Error")
  }, onCompleted: {
    print("RxSwift Timeout Finish")
  })
  .disposed(by: disposeBag)

// RxSwift Timeout Error

ReactiveSwift

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

let property = MutableProperty<Int>(0)

property.signal
  .timeout(after: 0.5, raising: error, on: QueueScheduler.main)
  .observe { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Timeout : \(value)")
    case .failed:
      print("ReactiveSwift Timeout Error")
    case .completed:
      print("ReactiveSwift Timeout Finish")
    default:
      break
    }
  }

// ReactiveSwift Timeout Error
PreviousPublishers.ZipNextPublishers.CompactMap

Last updated 5 years ago

Was this helpful?