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 thexcdatamodeld
file
To do this:
- Select the
xcdatamodeld
file inProject Navigator
- Open
Utilities
on the right side - Open
File Inspector
- 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 thexcdatamodeld
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
Sprite-Kit: Moving an Element in Circular Path
Resetting Zone Allocator with Allocations Still Alive
Swift, Avaudiorecorder: Error 317: Ca_Debug_String: Inpropertydata == Null
A Different Bridging Between Array and Dictionary
Swift: Overriding Didset Results in a Recursion
How to Subclass Custom Uiviewcontroller in Swift
Select Next Nstextfield with Tab Key in Swift
How to Add Auto-Complete Comment in Xcode (Swift)
Xcode: Could Not Load Modelio.Framework, Scenekit.Framework, etc
Stopping an Running Skaction - Sprite Kit
Swiftui @Environmentobject Error: May Be Missing as an Ancestor of This View
How to Make a Local Module with the Swift Package Manager
Proper Model for Multiple Alamofire Requests for Multiple Websites
How to Cancel Alamofire.Upload
Closure Containing a Declaration Cannot Be Used with Function Builder 'Viewbuilder'
Why Do Some Types (E.G. Float80) Have a Memory Alignment Bigger Than Word Size