Issue with Core Data not finding the correct entity

Dear everyone,


I am relatively new to Core Data and I was trying to create a small app in Swift UI to display a list of elements and being able to add new ones.


I created an entity (Codegen: Manual/None) with the following fields:

- city: String

- country: String

- createdAt: Date

- date: Date

- id: UUID

- summary: String


An I created the equivalent class:

public class EventItems: NSManagedObject, Identifiable {
    @NSManaged public var id: UUID
    @NSManaged public var city: String
    @NSManaged public var country: String
    @NSManaged public var date: Date
    @NSManaged public var createdAt: Date
    @NSManaged public var summary: String
}


And the following extension

extension EventItems {
    static func getAllItems() -> NSFetchRequest<EventItems> {
        let request: NSFetchRequest<EventItems> = NSFetchRequest<EventItems>(entityName: "EventItems")
        
        request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
        
        return request
    }
}


In a SwiftUI view, I set the Environement and then have a button to save a new element:

@Environment(\.managedObjectContext) var managedObjectContext


Button(action: {
    let eventItem:EventItems = EventItems(context: self.managedObjectContext)
    eventItem.id = UUID()
    eventItem.city = self.city
    eventItem.country = self.country
    eventItem.date = self.date
    eventItem.createdAt = Date()
    eventItem.summary = self.summary
    
    do {
        try self.managedObjectContext.save()
    } catch {
        print(error)
    }
}) {
    Text("Save")
}


In my View to add a new element to the entity, when I press the save button, the application crashes with the following error:

2019-10-20 20:50:04.209071+0200 EventLogger[14093:945375] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'EventLogger.EventItems' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?

CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'EventLogger.EventItems' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?

2019-10-20 20:50:04.209361+0200 EventLogger[14093:945375] [error] error: +[EventLogger.EventItems entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass

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

2019-10-20 20:50:04.219017+0200 EventLogger[14093:945375] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'EventLogger.EventItems' must have a valid NSEntityDescription.'


Thanks for your help 🙂

Replies

Hi,

I hope it's not too late for an answer.

Go to your CoreData model and click on your entity, in the data model inspector go to "Module" and click "Current Product Module".

That should resolve your problem.


Hope it helps! 😁

  • Thank you thank you thank you! you saved my *** :)

Add a Comment

Hi,


I am getting same similar error.. as below.. Hey hfgjd above, not sure how to find "Module" and to click "Current Product Module"..


Cant find that area and selection.


Hope you can help or send me a screen shot... please !


Thanks !




2020-05-02 09:27:50.813395+1000 Ham Log It[1148:40369] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Things' so +entity is unable to disambiguate.

CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Things' so +entity is unable to disambiguate.

2020-05-02 09:27:50.813787+1000 Ham Log It[1148:40369] [error] warning: 'Things' (0x600001b88160) from NSManagedObjectModel (0x600000f9f7f0) claims 'Things'.

CoreData: warning: 'Things' (0x600001b88160) from NSManagedObjectModel (0x600000f9f7f0) claims 'Things'.

2020-05-02 09:27:50.814055+1000 Ham Log It[1148:40369] [error] warning: 'Things' (0x600001b816b0) from NSManagedObjectModel (0x600000fa4f00) claims 'Things'.

CoreData: warning: 'Things' (0x600001b816b0) from NSManagedObjectModel (0x600000fa4f00) claims 'Things'.

2020-05-02 09:27:50.814425+1000 Ham Log It[1148:40369] [error] error: +[Things entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass

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

Hi Theshines... I am not an expert either... But if you follow what hfgjd said, it should work. Here is a screenshoot

  • In Xcode 15.4, it's under entity inspector (last tab), look under class, Module would be Global Namespace. Change it to Current Product Module.

    Note, it still crashes.

Add a Comment
As hfgjd replied, just set Module to Current Product Module works for me!
You can get this error several different ways. If you get it during fetch requests then you should try this:

@FetchRequest(entity: NSEntityDescription.entity(forEntityName: "Your Entity Name", in: managedObjectContext), sortDescriptors: [NSSortDescriptor(key: "Your Key", ascending: true)])

In dynamic fetch requests you should try this:

var entity: Entity
var fetchRequest: FetchRequest<Your Entity>
init(_ entity: Entity) {
self.entity = entity
self.fetchRequest = FetchRequest(entity: NSEntityDescription.entity(forEntityName: "Your Entity", in: managedObjectContext), sortDescriptors: [NSSortDescriptor(key: "Your Key", ascending: true)], predicate: NSPredicate(format: "entity.uniqueIdentifier == %@", entity.uniqueIdentifier!))
}
var youEntities: FetchedResults<Your Entity> {
fetchRequest.wrappedValue
}

When creating core data objects:

let entityDescription = NSEntityDescription.entity(forEntityName: "Your Entity", in: managedObjectContext)
let object = Your Entity(entity: entityDescription!, insertInto: managedObjectContext)

Hope this helps for your Core Data with SwiftUI issues.


  • Thanks for the help!

    I have two data files for my app. First one worked fine but the second gave me errors stating "multiple NSEntityDescriptions" and also "Failed to find a unique match for an NSEntityDescription to a managed object subclass". I added:

    **let entityDescription = NSEntityDescription.entity(forEntityName: "Your Entity", in: managedObjectContext) let object = Your Entity(entity: entityDescription!, insertInto: managedObjectContext) **

    This was added into the intialization of new entities and all of my errors and warnings went away!

Add a Comment
Hi,
I am getting this error when running unit tests in my actual code on:
Code Block Swift
try managedObjectContext.save()
=> Thread 1: hit Objective-C exception


Changing everything to one shared PersistenceController didn't help. Same error.

If I follow the hint to set "Current Product Module" on my core data models, I'm getting a different error already earlier when I create a new entity:

Code Block Swift
let player = Player(context: managedObjectContext)
=> Thread 1: "An NSManagedObject of class 'Player' must have a valid NSEntityDescription."

with the same log output as mentioned in OPs post.