반응형
Combine에서 자주 사용하는 Publisher와 같은 개념중에 구독자(subscriber)에게 전달하는데 사용되는
PassthroughSubject와 CurrentValueSubject는 값을 저장하느냐 마느냐의 차이점이 있습니다.
PassthroughSubject | CurrentValueSubject | |
값 저장 여부 | 저장하지 않음 | 저장 |
RxSwift에서 유사한 기능 | PublishSubject | BehaviorSubject |
- PassthroughSubject
값을 저장하지 않고 데이터를 방출(emit) 합니다.
let subject = PassthroughSubject<String, Never>()
subject.sink { value in
print(value)
}
subject.send("Hello, World!") // Hello, World! 출력
공식 문서 : https://developer.apple.com/documentation/combine/passthroughsubject
- CurrentValueSubject
값을 저장하고 데이터를 방출(emit) 합니다.
또한, 초기값을 지정해줘야 하며, value 프로퍼티에 값을 설정함으로써 send()와 동일한 동작하는것이 가능합니다.
let subject = CurrentValueSubject<String, Never>("Hello, World!")
subject.sink { value in
print(value)
}
// Hello, World! 출력
subject.send("Hello") // Hello 출력
subject.value = "World!" // World! 출력
공식문서 : https://developer.apple.com/documentation/combine/currentvaluesubject
반응형
'개발 > iOS' 카테고리의 다른 글
Warning "Non-constant range: argument must be an integer literal" (0) | 2023.12.07 |
---|---|
SFSymbol의 Monochrome, Hierarchical, Palette, Multicolor Mode (1) | 2023.12.05 |
Custom Environment (1) | 2023.12.05 |
@AppStorage에 Date 타입과 Array 타입 사용하기 (0) | 2023.12.04 |
UIView -> UIImage (0) | 2023.11.15 |
iOS 16.4 이후부터 WebView 디버깅 하기 (0) | 2023.11.15 |
iOS App URL Cache 제거 (0) | 2023.11.10 |
iOS 16이상에서 TextKit 1을 사용하고자 할때 (0) | 2023.10.22 |