Observable입니다. 샘플 코드에 NotificationCenter.Publisher가 소개되는데, nested struct로 정의한 Publisher가 Publisher protocol을 conform하고 있습니다. Foundation framework에서 바로 구현해주니 원래 있던 것 마냥 자연스럽습니다. Publisher protocol을 conform할 때는 associatedtype으로 Output과 Failure를 정의해야합니다. NotificationCenter.Publisher의 경우, Output은 Notification입니다.
extension NotificationCenter {
public func publisher(for name: Notification.Name, object: AnyObject? = nil) -> NotificationCenter.Publisher
}
Subscriber protocol을 conform해야하며, 이때 associatedtype으로 Input과 Failure를 정의해야합니다. Publisher의 Output 타입과 Subscriber의 Input 타입이 일치할 경우에만 구독이 가능합니다. Subscribers.Demand라는 struct를 통해, Publisher로부터 받을 수 있는 이벤트의 최대 갯수를 동적으로 늘려갈 수 있습니다.
Publisher.sink() 또는 Publisher.assign()의 리턴값이 이에 해당합니다. Subscriber를 명시적으로 만들지 않고 이벤트를 바로 처리할 수 있습니다.
Subscription protocol이 상속하고 있습니다. 명시적을 cancel()을 호출할 수도 있고, Subscription 객체가 deinit되는 시점에 구독이 중단됩니다.
PassthroughSubject와 CurrentValueSubject를 제공합니다. PassthroughSubject는 send()를 이용해 동적으로 이벤트를 방출할 수 있습니다. CurrentValueSubject은 초기값과 함께 생성되며, 현재의 값을 이벤트로 방출합니다.
Playground로 블랙잭 카드 딜러를 작성하는 과제입니다. Rx 이야기는 안 꺼내는 게 매너겠지만... RayWenderlich의 RxSwift를 같은 사람이 쓴 탓인지, Ch 2의 Challenge가 완전히 같습니다. 문제가 바로 이해되니 좋네요.
Combine. Ch 4. Filtering Operators (0) | 2020.08.23 |
---|---|
Combine. Ch 3. Transforming Operators (0) | 2020.08.17 |
Combine. Ch 1. Hello, Combine! (0) | 2020.04.26 |
댓글 영역