Empty

μ œλ„€λ¦­ ꡬ쑰체 | μ–΄λ– ν•œ 값도 μ ˆλŒ€ λ°œν–‰ν•˜μ§€ μ•ŠμœΌλ©°, μ„ νƒμ μœΌλ‘œ μ¦‰μ‹œ μ’…λ£Œν•˜λŠ” Publisher

두 개의 μ΄λ‹ˆμ…œλΌμ΄μ €λ₯Ό μ œκ³΅ν•œλ‹€.

  • init(completeImmediately:)

    • 인자의 기본값은 true이닀.

    • 인자의 값이 true이면 μ¦‰μ‹œ μ’…λ£Œν•˜κ³ , false이면 μ’…λ£Œν•˜μ§€ μ•ŠλŠ”λ‹€.

  • init(completeImmediately:outputType:failureType)

    • completeImmediately 인자의 기본값은 true이며, 값에 λ”°λ₯Έ λ™μž‘ λ³€ν™”λŠ” μœ„μ™€ κ°™λ‹€.

    • outputTypeκ³Ό failureType은 ν•΄λ‹Ή Publisherλ₯Ό Subscriberλ‚˜ λ‹€λ₯Έ Publisher와 μ—°κ²°ν•  λ•Œ μ—°κ²°ν•  λŒ€μƒμ˜ Output νƒ€μž…κ³Ό μ—λŸ¬ νƒ€μž…μ„ λ§žμΆ”κΈ° μœ„ν•΄ λͺ…μ‹œν•΄ μ€€λ‹€.

      • κ·ΈλŸ¬λ―€λ‘œ μΈμžμ— 메타 νƒ€μž…μ„ μ „λ‹¬ν•œλ‹€.

값을 μ „λ‹¬ν•˜κ³  싢지 μ•ŠμœΌλ©°, μ¦‰μ‹œ μ’…λ£Œν•˜κ±°λ‚˜ μ’…λ£Œν•˜μ§€ μ•ŠλŠ” Publisherλ₯Ό λ§Œλ“€κ³  싢을 λ•Œ μ‚¬μš©ν•œλ‹€.

// completeImmediately: true
Empty<Void, Never>()
  .sink(receiveCompletion: { _ in print("Combine Empty Finish") }, receiveValue: { })
  .store(in: &cancellables)

// Combine Empty Finish

// completeImmediately: false
Empty<Void, Never>(completeImmediately: false)
  .sink(receiveCompletion: { _ in print("Combine Never Finish") }, receiveValue: { })
  .store(in: &cancellables)

// 아무것도 좜λ ₯λ˜μ§€ μ•ŠμŒ

EmtpyλŠ” 값을 내지 μ•ŠμœΌλ―€λ‘œ receiveValue ν΄λ‘œμ €μ—μ„œλŠ” λ™μž‘μ„ 확인할 수 μ—†κ³ , receiveCompletion ν΄λ‘œμ €μ—μ„œ μ’…λ£Œν•˜λŠ” λ™μž‘μ„ 확인할 수 μžˆλ‹€.

1번 μ½”λ“œμ˜ 경우 Publisherκ°€ λ°”λ‘œ μ’…λ£Œν•˜μ—¬ receiveCompletion에 λ“±λ‘ν•œ ν΄λ‘œμ €κ°€ μ‹€ν–‰λ˜λŠ” 것을 확인할 수 μžˆλ‹€.

2번 μ½”λ“œμ˜ 경우 Publisherκ°€ μ’…λ£Œν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ•„λ¬΄λŸ° 좜λ ₯도 확인할 수 μ—†λ‹€.

RxSwift

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

  • empty μ˜€νΌλ ˆμ΄ν„°λŠ” Empty(completeImmediately: true)의 λ™μž‘κ³Ό κ°™λ‹€.

  • never μ˜€νΌλ ˆμ΄ν„°λŠ” Empty(completeImmediately: false)의 λ™μž‘κ³Ό κ°™λ‹€.

// empty Operator
Observable<Void>.empty()
  .subscribe(onCompleted: { print("RxSwift Empty Finish") })
  .disposed(by: disposeBag)

// RxSwift Empty Finish

// never Operator
Observable<Void>.never()
  .subscribe(onCompleted: { print("RxSwift Never Finish" )})
  .disposed(by: disposeBag)

// 아무것도 좜λ ₯λ˜μ§€ μ•ŠμŒ

ReactiveSwift

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

  • empty μ˜€νΌλ ˆμ΄ν„°λŠ” Empty(completeImmediately: true)의 λ™μž‘κ³Ό κ°™λ‹€.

  • never μ˜€νΌλ ˆμ΄ν„°λŠ” Empty(completeImmediately: false)의 λ™μž‘κ³Ό κ°™λ‹€.

// empty Operator
SignalProducer<Void, Never>.empty
  .startWithCompleted { print("ReactiveSwift Empty Finish") }

// ReactiveSwift Empty Finish

// never Operator
SignalProducer<Void, Never>.never
  .startWithCompleted { print("ReactiveSwift Never Finish") }
γ„Ή6 γ„±5γ…‡γ„·4γ„΄γ…ˆ333γ…‡γ„·4γ…ˆ2
// 아무것도 좜λ ₯λ˜μ§€ μ•ŠμŒ

μ°Έκ³ 

ReactiveX - Operators - Empty | Never

Last updated