0

I have following code in my SwiftUI app

struct ContentView: View {

@State private var selectedCountry: Country?
@State private var showSetting = false

@FetchRequest(entity: Country.entity(),
              sortDescriptors: [NSSortDescriptor(keyPath: \Country.cntryName, ascending: true)]
) var countries: FetchedResults<Country>

var body: some View {
    NavigationView {
        VStack {
            Form {
                Picker("Pick a country", selection: $selectedCountry) {
                    ForEach(countries, id: \Country.cntryName) { country in
                        Text(country.cntryName ?? "Error").tag(country as Country?)
                    }
                }
                if selectedCountry != nil {
                    DetailView(cntryName: (selectedCountry?.cntryName!)!)
                }
            }
        }
        .navigationBarTitle("UNECE Data")
        .navigationBarItems(trailing: Button("Settings", action: {
            self.showSetting.toggle()
        }))
    }
    .sheet(isPresented: $showSetting) {
        SettingsView(showSetting: self.$showSetting)
    }
}
}

I do CoreData Country entity update in SettingView and once app is back in ContentView I`d like to delete all items from the Picker and load fresh data. Code above duplicate items in the Picker - add new ones to old set.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dawy
  • 770
  • 6
  • 23
  • Picker for dynamic set of values? see https://stackoverflow.com/questions/60082411/swiftui-hierarchical-picker-with-dynamic-data-crashes/60083331#60083331 – user3441734 Feb 18 '20 at 18:13
  • My issue is that when SettingsView is dismissed then Picker loads countries FetchedResults but it double items in the Picker - originally loaded FetchedResults + newly loaded. – Dawy Feb 20 '20 at 20:49

0 Answers0