I am currently working with CoreData FetchedResults and SwiftUIs PickerView. While doing so I encountered a problem when using the FetchedResults to populate a PickerView:
I obviously can use the FetchedResults to populate the PickerView but what solution is the best (also from UI / UX perspective) for defining the @State for the selected Result?
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \User.lastName, ascending: true)], animation: .default) private var users: FetchedResults<User>
@State var user: User = User()
// ...
Picker(selection: $user, label: Text("User")) {
ForEach(users) { user in
Text(lastName).tag(user)
}
}
This does work, however it is probably not the most elegant way to create a new empty User Object to set it as the default.
Does anyone of you have a better Idea / Best Practice for solving this probably often problem?