RxSwift로 프로젝트를 진행하고 공부를 하다가 WWDC를 보고 SwiftUI를 공부해보면 재밌겠다고 생각했다.
풍문으로 듣기로는 SwiftUI와 combine이 합이 잘맞는다고 들었다.
비교해보며 정말 간단하게
공부해보자.
공식문서가 있다. 와 ...
비동기 작업 어쩌고 설명이 되어있는 모습이다.
Anypublisher vs Observable
비슷한 개념인 것 같다.
설명을 보면
AnyPublisher is a concrete implementation of Publisher that has no significant properties of its own, and passes through elements and completion values from its upstream publisher.
우리가 RxSwift에서 subject 나 relay의 값을 observable로 감싸줄때 asObservable()
을 사용했다면
You can use Combine’s eraseToAnyPublisher() operator to wrap a publisher with AnyPublisher.
eraseToAnyPublisher()
를 사용하라고 적혀있다 !
미래? 뭘까? 설명을 보니 result 타입으로 해주는 무언가 같다. Single이랑 비슷한건가 ? 이건 나중에 알아보자 나중에.
Just, RxSwift의 그 Just같다.
자 구독을 해줄 차례다.
RxSwift에서는 subscribe를 해주었는데, Combine은 어떨까?
이런게 있다.
Overview
A Subscriber instance receives a stream of elements from a Publisher, along with life cycle events describing changes to their relationship. A given subscriber’s Input and Failure associated types must match the Output and Failure of its corresponding publisher.
You connect a subscriber to a publisher by calling the publisher’s subscribe(:) method. After making this call, the publisher invokes the subscriber’s receive(subscription:) method. This gives the subscriber a Subscription instance, which it uses to demand elements from the publisher, and to optionally cancel the subscription. After the subscriber makes an initial demand, the publisher calls receive(:), possibly asynchronously, to deliver newly-published elements. If the publisher stops publishing, it calls receive(completion:), using a parameter of type Subscribers.Completion to indicate whether publishing completes normally or with an error.
Combine provides the following subscribers as operators on the Publisher type:
- sink(receiveCompletion:receiveValue:) executes arbitrary closures when it receives a completion signal and each time it receives a new element.
- assign(to:on:) writes each newly-received value to a property identified by a key path on a given instance.
읽어보면 publisher의 스트림의 요소를 가져올 수 있다. Input이나 Failure 중 하나를 가져온다.
오 이거 RxSwift의 Subscribe 같다.
두번째 단락을 읽어보면 subscribe를 하면 receive
한단다. 이게 Subscription을 가져온다고 한다.
Subscription은 publisher의 요소란다.
마지막 단락에 sink
랑 assign
이 있다.
RxSwift도 bind
랑 drive
같은게 있었으니 비슷한거 아닐까 ? 잘 모른다 다음에 알아보자 다음에.
정말 간단하게 알아보았다.
세줄요약:
1. observable -> anyPublisher
2. asObservable -> erase ~~
3. 구독시 그 요소랑 완료했는지를 받아볼 수 있음