Skip to content

Instantly share code, notes, and snippets.

@michzio
Created December 12, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michzio/10a7c4af567b0b0f078f161ae71208a6 to your computer and use it in GitHub Desktop.
Save michzio/10a7c4af567b0b0f078f161ae71208a6 to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - fetching cached data with refreshing
public func fetchWordsetCategories() -> AnyPublisher<[WordsetCategory], Error> {
let cachedDataPublisher = dao.fetchCategories()
let refreshedDataPublisher = service.getCategories(from: "pl", to: "en")
.flatMap { [weak self] categories in
self?.dao.update(categories)
}
.eraseToAnyPublisher()
return Publishers.MergeMany(cachedDataPublisher, refreshedDataPublisher)
.eraseToAnyPublisher()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment