Post

Replies

Boosts

Views

Activity

SWIFTUI Core Data Object Return from Picker
I've been working this problem for a couple of days and decided to ask for help. The use case is that I have two Core Data Entities (in CloudKit) which have a relationship between them. In the updating of one entity, I am trying to add the related entity by adding the result from a user selection, via a picker. The issue I am facing is that I can't get the picker to return the record as the selection - it's always null. I have developed the following test code to highlight the specific case. The picker populates and I can make it return anything other than the actual record - but to insert in the relationship, I need the record itself. Here is the picker test code. Selection is ALWAYS empty. I would appreciate direction. Craig struct TestView: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Player.familyName, ascending: true)], animation: .default) private var players: FetchedResultsPlayer @State var selection: Player? var body: some View { VStack{ Picker("", selection: $selection){ ForEach(players, id: \.self){ (player: Player) in Text(player.givenName!).tag(player.self) } } Text(((selection?.familyName ?? "default"))) Text("\(players.count)") } } }
4
2
4.8k
Feb ’22
SWIFTUI Core Data Object Return from Picker
I've been working this problem for a couple of days and decided to ask for help. The use case is that I have two Core Data Entities (in CloudKit) which have a relationship between them. In the updating of one entity, I am trying to add the related entity by adding the result from a user selection, via a picker. The issue I am facing is that I can't get the picker to return the record as the selection - it's always null. I have developed the following test code to highlight the specific case. The picker populates and I can make it return anything other than the actual record - but to insert in the relationship, I need the record itself. Here is the picker test code. Selection is ALWAYS empty. I would appreciate direction. Craig struct TestView: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Player.familyName, ascending: true)], animation: .default) private var players: FetchedResultsPlayer @State var selection: Player? var body: some View { VStack{ Picker("", selection: $selection){ ForEach(players, id: \.self){ (player: Player) in Text(player.givenName!).tag(player.self) } } Text(((selection?.familyName ?? "default"))) Text("\(players.count)") } } }
Replies
4
Boosts
2
Views
4.8k
Activity
Feb ’22