How to weak link Combine in macOS 10.14 and iOS 12

Issue #593 #if canImport(Combine) is not enough, need to specify in Other Linker Flags OTHER_LDFLAGS = -weak_framework Combine Read more https://stackoverflow.com/questions/57168931/optional-linking-for-swift-combine-framework-in-xcode-11

February 2, 2020 · 1 min · 21 words · Khoa

How to convert from callback to Future Publisher in Combine

Issue #527 import Foundation import Combine public typealias TaskCompletion = (Result<(), Error>) -> Void public protocol Task: AnyObject { var name: String { get } func run(workflow: Workflow, completion: TaskCompletion) } public extension Task { func asPublisher(workflow: Workflow) -> AnyPublisher<(), Error> { return Future({ completion in self.run(workflow: workflow, completion: completion) }).eraseToAnyPublisher() } } let sequence = Publishers.Sequence<[AnyPublisher<(), Error>], Error>( sequence: tasks.map({ $0.asPublisher(workflow: self) }) )

December 2, 2019 · 1 min · 65 words · Khoa

How to use objectWillChange in Combine

Issue #513 A publisher that emits before the object has changed Use workaround DispatchQueue to wait another run loop to access newValue .onReceive(store.objectWillChange, perform: { DispatchQueue.main.async { self.reload() } }) func reload() { self.isFavorite = store.isFavorite(country: country) } Read more https://forums.swift.org/t/combine-observableobject-in-uikit/28433 https://twitter.com/luka_bernardi/status/1155944329363349504

November 21, 2019 · 1 min · 42 words · Khoa

How to map error in Combine

Issue #506 When a function expects AnyPublisher<[Book], Error> but in mock, we have Just func getBooks() -> AnyPublisher<[Book], Error> { return Just([ Book(id: "1", name: "Book 1"), Book(id: "2", name: "Book 2"), ]) .eraseToAnyPublisher() } There will be a mismatch, hence compile error Cannot convert return expression of type ‘AnyPublisher<[Book], Just.Failure>’ (aka ‘AnyPublisher<Array, Never>’) to return type ‘AnyPublisher<[Book], Error>’ The reason is because Just produces Never, not Error. The workaround is to introduce Error...

November 14, 2019 · 1 min · 107 words · Khoa

How to flat map publisher of publishers in Combine

Issue #451 For some services, we need to deal with separated APIs for getting ids and getting detail based on id. To chain requests, we can use flatMap and Sequence, then collect to wait and get all elements in a single publish FlatMap Transforms all elements from an upstream publisher into a new or existing publisher. struct FlatMap<NewPublisher, Upstream> where NewPublisher : Publisher, Upstream : Publisher, NewPublisher.Failure == Upstream.Failure Publishers.Sequence...

October 9, 2019 · 1 min · 191 words · Khoa