이 글에서는 Subscriber개념을 Publisher 메서드와 함께 묶어 설명합니다.

아직 Publisher에 대해 잘 모른다면 Publisher란? 을 읽고 돌아와주세요

listening.jpeg

Subscriber은 Publisher로부터 이벤트나 값을 전달받아 구독하는 객체에요.

protocol Subscirber을 보면 두 가지 타입과 세 가지 메서드가 있어요.

public protocol Subscriber<Input, Failure> : CustomCombineIdentifierConvertible {

    associatedtype Input

    associatedtype Failure : Error
    
    func receive(subscription: any Subscription)

    func receive(_ input: Self.Input) -> Subscribers.Demand

    func receive(completion: Subscribers.Completion<Self.Failure>)
}

먼저 Input과 Failure은 Publisher로 부터 받는 출력(Output)과 Failure이에요.

public protocol Publisher<Output, Failure> {

    associatedtype Output

    associatedtype Failure : Error
	 
		...   
}

때문에 Subscriber의 Input과 Failure은 Publisher의 Output과 Failure의 타입과 일치해야 해요.

이제 Subscriber에 선언되어 있는 세 가지 메서드를 볼게요.