Skip to content

Instantly share code, notes, and snippets.

@michzio
Created December 12, 2021 14:49
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/17851ff3cb40025a5c603e6ed0b408d3 to your computer and use it in GitHub Desktop.
Save michzio/17851ff3cb40025a5c603e6ed0b408d3 to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - ViewModel simplified
@Published var categories: [WorsetCategory] = []
@Published var error: Error?
@Published var isLoading: Bool = false
func fetchWordsetCategories() {
guard !isLoading else { return }
repo.fetchWordsetCategories()
.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
self?.error = error
case .finished:
break
}
self?.isLoading = false
}) { [weak self] categories in
self?.categories = categories
}
.store(in: &disposables)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment