Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've created simple entity 'CDWorkout' with one attribute 'name' inside CDModel.xcdatamodeld. Name of container in AppDelegate is also 'CDModel'. Class Codegen for 'CDWorkout' is Category/Extension. Here is code for CDWorkout class:

class CDWorkout: NSManagedObject {      class func createWorkout(workoutInfo : Workout, in context: NSManagedObjectContext) -> CDWorkout{         let workout = CDWorkout(context: context)         workout.name = "anyName"         return workout     } } 

the createWorkout function is called from another viewController with context argument as container.viewContext but it immediately crashes with message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'Workout_Generator.CDWorkout' must have a valid NSEntityDescription.'

What did i forget about?

like image 676
barola_mes Avatar asked Aug 01 '17 09:08

barola_mes


2 Answers

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.

like image 112
barola_mes Avatar answered Sep 22 '22 01:09

barola_mes


I had a silly minor issue that resulted in the same error. I was initializing NSPersistentContainer with the incorrect name.

It should have the same name as the source file with the extension .xcdatamodeld. e.g. modelFileName.xcdatamodelId

let persistentContainer = NSPersistentContainer(name: "modelFileName") 
like image 41
nikdange_me Avatar answered Sep 21 '22 01:09

nikdange_me