Post

Replies

Boosts

Views

Activity

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: StringAn 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 extensionextension 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 managedObjectContextButton(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 subclassCoreData: error: +[EventLogger.EventItems entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass2019-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 🙂
6
0
9.4k
Oct ’19