How to update @FetchRequest, when a related Entity changes in SwiftUI?

I also struggled with this and found a very nice and clean solution: You have to wrap the row in a separate view and use @ObservedObject in that row view on the entity. Here’s my code: WineList: struct WineList: View { @FetchRequest(entity: Wine.entity(), sortDescriptors: [ NSSortDescriptor(keyPath: \Wine.name, ascending: true) ] ) var wines: FetchedResults<Wine> var … Read more

Combine framework: how to process each element of array asynchronously before proceeding

With your latest edit and this comment below: I literally am asking is there a Combine equivalent of “don’t proceed to the next step until this step, involving multiple asynchronous steps, has finished” I think this pattern can be achieved with .flatMap to an array publisher (Publishers.Sequence), which emits one-by-one and completes, followed by whatever … Read more

Combine framework serialize async operations

Use flatMap(maxPublishers:transform:) with .max(1), e.g. func imagesPublisher(for urls: [URL]) -> AnyPublisher<UIImage, URLError> { Publishers.Sequence(sequence: urls.map { self.imagePublisher(for: $0) }) .flatMap(maxPublishers: .max(1)) { $0 } .eraseToAnyPublisher() } Where func imagePublisher(for url: URL) -> AnyPublisher<UIImage, URLError> { URLSession.shared.dataTaskPublisher(for: url) .compactMap { UIImage(data: $0.data) } .receive(on: RunLoop.main) .eraseToAnyPublisher() } and var imageRequests: AnyCancellable? func fetchImages() { imageRequests = … Read more

How to tell SwiftUI views to bind to nested ObservableObjects

Nested models does not work yet in SwiftUI, but you could do something like this class SubModel: ObservableObject { @Published var count = 0 } class AppModel: ObservableObject { @Published var submodel: SubModel = SubModel() var anyCancellable: AnyCancellable? = nil init() { anyCancellable = submodel.objectWillChange.sink { [weak self] (_) in self?.objectWillChange.send() } } } Basically … Read more