Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 538 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'An NSManagedObject of class 'className' must have a valid NSEntityDescription.' error

#1
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?

Reply

#2
What fixed it for me was that I had an empty space in the name and didn't notice because of a line break. Removing it fixed the problem..

It was:

container = `NSPersistentContainer(name: "ContainerName ")`

Instead of:

container = NSPersistentContainer(name: "ContainerName")

Reply

#3
For me this worked,

Adding

@objc('YourEntityClassName')

above the class declaration in 'YourEntityClassName+CoreDataClass' file.

Note: I am using this in an SPM package.
Reply

#4
In my case, I had my Core Data model inside a Swift Package and had to follow [this][1] amazing guide and finally this is what did it:

I opened the model and selected the entity and in the right panel I selected the `Data Model Inspector` and in the Class panel, I manually entered the Module’s name i.e. the Swift Package name.

[1]:

[To see links please register here]

Reply

#5
In `AppDelegate` check persistent container name.

```swift
let container = NSPersistentContainer(name: "CoreData")
````

If you trying to reach nonexistent `Core Data`, that may manifest as this error.

Name should be exact same as `.xcdatamodeld` object in navigation menu.
Reply

#6
I've got the same issue. In my case I used CoreData initialised and managed in framework wich I integrated with main app via **SPM**.

## Solution ##

First, in the framework provide module name for entity by hand, by following steps:
1. open .xcdatamodel,
2. select Entity which you want to edit,
3. open **Data Model Inspector** (the 4th panel in right sidebar),
4. at section **Class** element Module by default presents **Current Product Module** -- enter here your Module name

Repeat these steps for each entity.

Second, in the framework override `description` for each NSManagedObject you use in project, f.ex. you have:

public class Person: NSManagedObject {

}

override there description with String without module name, like:

public class Person: NSManagedObject {

public override var description: String {
return "Person"
}
}

Code presented above will help if you use convenience initialiser for Person (like `Person(context: Context)`) which uses `description` to specify `Person.entity()`.
Reply

#7
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.
Reply

#8
I encountered this issue using Swift Package Manager to import a Swift Package that had a model file included as a resource. In my case setting the Module of my entity to `Current Production Module` produced an incorrect value in the model file. When I used the debugger to print the model after loading the persistent container the fully qualified class name was something like `Module_Module.Class` instead of the expected `Module.Class`. I had to manually type the module name instead of using `Current Production Module` to resolve the issue.
Reply

#9
For me, I forgot to add @objc(Reminder) in this below example. I wrote the NSManagedObject class programmatically.

@objc(Reminder)
public class Reminder: NSManagedObject {


}
Reply

#10
One way this could happen is by trying to rename your entities. Make sure that these are the same!

<img src="https://i.stack.imgur.com/86z4N.png" width="600">
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through