0

Here is how I add new entity.

func addCountry(name: String, code: String, flagImageUri: String?, wikiDataId: String) {
    let newCountry = CountryEntity(context: container.viewContext)
    newCountry.name = name
    newCountry.code = code
    newCountry.flagImageUri = flagImageUri
    newCountry.wikiDataId = wikiDataId
    save()
}

Here is my data:

enter image description here

However when I use the add function in my view, I got this error:

CoreData: error: +[CountryEntity entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass

And this is my button:

Button(action: {
            country.isFaved = !country.isFaved
            coreDataModel.addCountry(name: country.name, code: country.code, flagImageUri: country.flagImageUri, wikiDataId: country.wikiDataId)
        }) {
            Image(systemName: "star.fill")
                .foregroundColor(country.isFaved ? .black : .white)
                .scaledToFit()
        }

This is the whole class. I'm fetching, saving ,adding and deleting all data here. I did everything like the video I watched in youtube.

class DataController: ObservableObject {
let container = NSPersistentContainer(name: "CountryCoreData")
@Published var savedCountries: [CountryEntity] = []

init() {
    container.loadPersistentStores(completionHandler: { _, error in

        if let error = error {
            print("CoreData failed to load: \(error.localizedDescription)")
        } else {
            print("Successfully loaded")
        }

    })
}

func fetchCountries() -> [CountryEntity]? {
    let request = NSFetchRequest<CountryEntity>(entityName: "CountryEntity")

    do {
        let fetchedCountries = try container.viewContext.fetch(request)
        return fetchedCountries
    } catch {
        print("Something went wrong while data fetching \(error)")
        return nil
    }
}

func delete(code: String) {
    guard let fetchedCountries = fetchCountries() else { return }

    for country in fetchedCountries {
        if country.code!.contains(code) {
            container.viewContext.delete(country)
            save()
        }
    }
}

func addCountry(name: String, code: String, flagImageUri: String?, wikiDataId: String) {
    let newCountry = CountryEntity(context: container.viewContext)
    print("OSMAN")
    newCountry.name = name
    newCountry.code = code
    newCountry.flagImageUri = flagImageUri
    newCountry.wikiDataId = wikiDataId
    save()
}

func save() {
    do {
        try container.viewContext.save()
        fetchCountries()
    } catch {
        print("Error while saving the data: \(error)")
    }
}

}

How can I solve this problem?

themmfa
  • 499
  • 4
  • 15
  • @NiravD actually I did, but could not find how to solve my problem – themmfa Oct 08 '22 at 09:10
  • Add code where you have declare `NSPersistentContainer` – Nirav D Oct 08 '22 at 09:25
  • @NiravD I just added the whole class. You can check – themmfa Oct 08 '22 at 09:29
  • Can you show where you have created the object of `DataController` also check that you are not creating it on multiple places – Nirav D Oct 08 '22 at 09:32
  • @NiravD I think I understand where I'm doing wrong. I'm creating the DataController in a subview and that subview creates new DataController object everytime i create that new subview. – themmfa Oct 08 '22 at 09:37
  • 1
    Does this answer your question? [Failed to find a unique match for an NSEntityDescription CoreData Swiftui](https://stackoverflow.com/questions/64826285/failed-to-find-a-unique-match-for-an-nsentitydescription-coredata-swiftui) – Nirav D Oct 08 '22 at 09:50
  • This was happening because I was iterating to create new subviews and every subview creates new DataController – themmfa Oct 08 '22 at 14:37
  • 1
    So it's the same issue, right? – Nirav D Oct 08 '22 at 14:40
  • Yes it was @NiravD – themmfa Oct 08 '22 at 14:41

0 Answers0