UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to create a picker from the unique values in an attribute of an entity from CORE Data?

Forums > SwiftUI

I have a Core Data app which I am trying to create pickers in order to help the users filter the core data objects to more quickly find the object they are actually looking for. Two of the filters are based on a core data relationship and those I have working because they are always unique values in the related core data entity. The other two attributes (year collected and seed source) are based on user entered values, and so there could be 1 to n unique values. If I use the fetch request to populate the picker, I get all of the values not just the unique ones, which really makes it useless as a filter.

I am extremely new to SwiftUI and to asking for help in a forum so please let me know if I need to provide different/better information.

struct CrossListView: View {

@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: [
    SortDescriptor(\.name)
]) var dahlias: FetchedResults<Dahlia>

@FetchRequest(sortDescriptors: [
    SortDescriptor(\.name)
], predicate: NSPredicate(format: "name == %@", "Open Pollination")) var openPollination: FetchedResults<Dahlia>

private var activeDahlias: [Dahlia] {
    return dahlias.filter { r in
        return r.archive == false && r.parent == true
    }
}

@State public var seedParent: Dahlia?
@State public var pollenParent: Dahlia?

@FetchRequest(sortDescriptors: [
    SortDescriptor(\.yearCollected)
]) var crosses: FetchedResults<Cross>

@State private var seedFilter = ""
@State private var pollenFilter = ""
@State private var yearFilter = ""
@State private var seedSourceFilter = ""

var body: some View {
    NavigationView{
        VStack{ 
                Form{
                    if showingFilterSection == false {
                        Section(header:
                            Button {
                                    showingFilterSection.toggle()
                            } label: {
                                HStack {
                                    Image(systemName: "plus.rectangle")
                                    Text("SHOW FILTERS")
                                }
                        })
                        {
                            EmptyView()
                        }
                    } else {
                    Section(header:
                        Button {
                                showingFilterSection.toggle()
                        seedFilter = ""
                        pollenFilter = ""
                        yearFilter = ""
                        seedSourceFilter = ""

                        } label: {
                            HStack {
                                Image(systemName: "minus.rectangle")
                                Text("HIDE FILTERS")
                            }
                    })
                    {
                        Picker("Seed Parent", selection: $seedFilter) {
                            Text("All").tag("All")
                            ForEach(activeDahlias.sorted(), id: \.self) { (activeDahlias: Dahlia) in
                                Text(activeDahlias.wrappedName).tag(activeDahlias.wrappedName as String)
                            }
                        }
                        Picker("Pollen Parent", selection: $pollenFilter) {
                            Text("All").tag("All")
                            ForEach(activeDahlias.sorted(), id: \.self) { (activeDahlias: Dahlia) in
                                Text(activeDahlias.wrappedName).tag(activeDahlias.wrappedName as String)
                            }
                        }
                        Picker("Year Collected", selection: $yearFilter) {
                            Text("All").tag("All")
                            ForEach(crosses, id: \.self) { (cross: Cross) in
                                Text(String(cross.yearCollected)).tag(cross.yearCollected)
                            }
                        }
                        Picker("Seed Source", selection: $seedSourceFilter) {
                            Text("All").tag("All")
                            ForEach(activeDahlias.sorted(), id: \.self) { (activeDahlias: Dahlia) in
                                Text(activeDahlias.wrappedName).tag(activeDahlias.wrappedName as String)
                            }
                        }

                    }
                    }
                    List{
                    ForEach(crosses) {cross in
                        NavigationLink { 
                        // and then here is where the list generates

1      

Look at this thread @roosterboy's post could help.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.