반응형

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

 

 

 

반응형
Posted by 까칠코더
,