dbDev
dbDev

Reputation: 1549

Core Data results into a SwiftUI Picker view without preselecting an option

I am fetching data from Core Data and displaying it in a Picker, this view is built in SwiftUI. I struggled at first to get the selection to work from the picker, but found if I preselect the first object from my fetch result, in the view's init, the picker selection works.

What I want is to be able to load my data into the Picker, but not preselect one of the objects. Again, if I update the code below so nothing is preselected, then I can't select anything. I get to the screen of options, but when I tap one, nothing happens, I just get that little flash on the row. I have to hit the Back button to get back to the form view.

Any ideas for how to make loading Core Data into a Picker without preselecting one of the options work?

-Thanks!

import SwiftUI
import CoreData

struct RecordCreateview: View {
    @FetchRequest private var actions: FetchedResults<Actions>
    @State private var selectedAction: Actions

    init(context: NSManagedObjectContext) {
        let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
        fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
        fetchRequest.predicate = NSPredicate(value: true)
        self._actions = FetchRequest(fetchRequest: fetchRequest)
        do {
            let fetchResult = try context.fetch(fetchRequest)
            self._selectedAction = State(initialValue: fetchResult[0])
        } catch {
            fatalError("Problem fetching Action records.")
        }
    }
    
    var body: some View {
        NavigationView {
            Form {
                Picker("Select action", selection: $selectedAction){
                    ForEach(actions) { action in
                        if action.title == true {
                            Text("\(action.action!)").tag(action)
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 897

Answers (1)

You could set the selectedAction to a non-existent Actions in init(...) like this:

self._selectedAction = State(initialValue: Actions(context: context))

that will not set a pre-selected object in the picker.

struct RecordCreateview: View {
    @FetchRequest private var actions: FetchedResults<Actions>
    @State private var selectedAction: Actions

    init(context: NSManagedObjectContext) {
        let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
        fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
        fetchRequest.predicate = NSPredicate(value: true)
        self._actions = FetchRequest(fetchRequest: fetchRequest)
        self._selectedAction = State(initialValue: Actions(context: context))  // <--- here
    }
    
    var body: some View {
        NavigationView {
            Form {
                Picker("Select action", selection: $selectedAction){
                    ForEach(actions) { action in
                        if action.title == true {
                            Text("\(action.action!)").tag(action)
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions