Skip to content

Instantly share code, notes, and snippets.

@michzio
Last active December 12, 2021 10:34
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/133dca25097ec836687acca68ff85bdb to your computer and use it in GitHub Desktop.
Save michzio/133dca25097ec836687acca68ff85bdb to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - View
struct WordsetCategoriesView<VM: WordsetCategoriesViewModelProtocol>: View {
// MARK: - View Model
@StateObject private var viewModel: VM
// MARK: - Core Data
@FetchRequest private var categories: FetchResults<WordsetCategory>
init(viewModel: VM) {
_viewModel = viewModel
_categories = FetchRequest(
entity: WordsetCategory.entity(),
sortDescriptors: [
NSSortDescriptor(key: "foreignName", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
]
)
}
var body: some View {
List {
ForEach(Array(categories.enumerated()), id: \.1) { i, category in
Text("\(category.foreignName)")
}
}
.onAppear {
viewModel.syncWordsetCategories()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment