MacKuba

Kuba Suder's blog on Mac & iOS development

Combine in Practice

Categories: Foundation, WWDC 19 0 comments Watch the video

try*** variants (tryMap etc.) – accepts a throwing closure, captures thrown errors and converts them to stream failures

.decode(User.self, JSONDecoder()) -> <User, Error>

.assertNoFailure() -> <T, Never> – asserts if it receives a failure

Just(value) – just publishes this single value

.catch { otherPublisher } – if it receives an error, cancels the connection and subscribes to the fallback publisher instead

.flatMap { otherPublisher } – maps all values & errors to this publisher, returns whatever it outputs

.publisher(for: \.name) – like map { $0.name } ?…

.receive(on: RunLoop.main)

Subscribers:

.sink { x in … }

Subject: both subscriber and publisher, broadcasts to multiple subscribers, you can send() manually

PassthroughSubject: stores no value, so you’ll only see values some time after you subscribe

CurrentValue: stores last value, allows new subscribers to catch up

@Published var password: String  ⭢  property wrapper that adds a publisher

Future { promise in … promise(.success(data)) }  ⭢  publisher that does something asynchronously and returns a result once