Learn Courses My Dashboard

Core Data - how to initiate my App settings properly?

Hello together,

i want to initiate my App settings by using core data.

E.g. the country - a list, which content fetched from a Firestore database, shall be initiated by the users previous selection. The storage in core data works well, I do have problems with the initiation of the pickers binding.

Please look at the following code snippets. Hopefully the seniors can answer resp. help me immediately … :grinning:

Core Data entitites:

The AppSettings model:

import Foundation
import SwiftUI

class AppSettings: ObservableObject {
    
    // MARK: properties
    @Published var country: String

    
    init(country: String = "Österreich") {
        self.country = country
    }
}

The ScoreboardSettings+CoreData Class

import Foundation
import CoreData

@objc (ScoreboardSettings)
public class ScoreboardSettings: NSManagedObject {

}

The Settings View

import SwiftUI
import CoreData


struct SettingsView: View {
    
    // Acces viewContext to use Core Data
    @Environment(\.managedObjectContext) private var viewContext
    
    @EnvironmentObject var settings: AppSettings // populated by a Firestore database 
    @EnvironmentObject var model: ContentModel
    
    @FetchRequest(sortDescriptors: []) var coreData: FetchedResults<ScoreboardSettings>
    
    var body: some View {
        NavigationView {
                Form {
                    Section {
                        Picker ("Country", selection: $settings.country) {
                            ForEach(countries, id: \.self) {
                                Text($0)
                            }
                        }
                    }
                }
        }
        .onAppear {
            // parse database result and create list with supported countries
            for i in 0...model.countries.count-1 {
                countries.append(model.countries[i].name)
            }
            
            // fetch stored core data values
            initialzeAppsettingsWithCoreDataValues()
                    
        }
        .onDisappear{
            // store Appsettings in CoreData
            storeSettingsToCoreData(arg: settings)
        }
    }

    private func storeSettingsToCoreData(arg: AppSettings) {
        
        let core = ScoreboardSettings(context: viewContext)
        
        core.country = arg.country
        
        do {
            try viewContext.save()
        }
        catch {
            // Error with saving
        }
    }
    
    func initialzeAppsettingsWithCoreDataValues() {
        
        if coreData.country != nil {
            settings.country = coreData.country!
        }     
    }
}

The initializeApssetingsWithCoreDataValues() doesn’t work. I get the following error messages:

Referencing subscript ‘subscript(dynamicMember:)’ requires wrapper ‘Binding<FetchRequest.Configuration>’
Value of type ‘FetchedResults’ has no dynamic member ‘country’ using key path from root type ‘FetchRequest.Configuration’

Now my question:

how to connect the binding $settings.country with the fetched value from coreSettings.country? Or is there a better way to program?

Thanks and best regards

Peter