An Nsmanagedobject of Class 'Classname' Must Have a Valid Nsentitydescription.' Error

An NSManagedObject of class 'className' must have a valid NSEntityDescription.' error

The issue I had was that I needed to have the Class Module set to Current Product Module for my CDWorkout Entity.

In Xcode 10 there is a drop down in the Class section of the Data Model Inspector.

How to fix An NSManagedObject of class 'ClassName' must have a valid NSEntityDescription when using CoreData from a framework

What helped:

  • checking all targets that will use CoreData for the xcdatamodeld file

To do this:

  1. Select the xcdatamodeld file in Project Navigator
  2. Open Utilities on the right side
  3. Open File Inspector
  4. Check all needed targets under Target Membership

Must have a valid NSEntityDescription (SwiftUI)

Could you check if the following works:

Check the NSPersistentContainer name

  • What is the NSPersistentContainer name you are using (check initializer)? Does it match the xcdatamodeld file name?

Check the App Code

  • It is very important to initialise PersistenceController before using it.
  • So that the entities are loaded.
    If you are using the SwiftUI App life cycle then check the following:

Example:

import SwiftUI

@main
struct TestCoreDataApp: App {
//This is very important to initialise `PersistenceController` before using it. So that the entities are loaded.
let persistenceController = PersistenceController.shared

var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}

Clear Cache:

  • Xcode caches the entity classes and sometimes that could cause issues.
  • Try Xcode > Product > Clean Build Folder (Command Shift K). (or remove contents of DerivedData folder).
  • Delete the app on the simulator / device
  • Quit Xcode and re-open and run.

Sample Project:

  • It might help to use the sample CoreData Project if you are new to CoreData.
  • Xcode > New Project > Check the CoreData checkbox

Documentation

It might help to go through CoreData documentation. Yes it can be time consuming but it might help in the long run.

  • https://developer.apple.com/documentation/coredata
  • Sample Xcode project

Sigabrt with error that class must have a valid NSEntityDescription

Try commenting this line @objc (Trancsaction)

//@objc (Trancsaction)
public class Trancsaction: NSManagedObject {

An NSManagedObject of class 'NSManagedObject' must have a valid NSEntityDescription

The issue here is that you are executing [vedvc saveData] before displaying the vedvc view controller. At this point, vedvc has not been displayed, so its viewDidLoad has not yet been executed. Consequently self.ad and self.VehicleNumberED are both nil. Hence the error: the entity description is nil.

The simplest fix would be to move the initialisation of those two variables (ad and VehicleNumberED) to the saveData method. But you might do better to rethink your code structure.

Core Data NSManagedObject doesn't have a valid NSEntityDescription

Try changing:

let container = NSPersistentContainer(name: "DELETE")

to this:

let container = NSPersistentContainer(name: "StudyHub")

According to documentation:

By default, the provided name value is used to name the persistent store and is used to look up the name of the NSManagedObjectModel object to be used with the NSPersistentContainer object.



Related Topics



Leave a reply



Submit