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

#11
For SwiftUI you need to also update this method from SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//This gets the context from AppDelegate and sets is as moc environment variable
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = ContentView().environment(\.managedObjectContext, context)
//...
}



Reply

#12
If you are using coreData from a static library make sure you specify the module aswell

[![enter image description here][1]][1]

This gives you the correct entity access `MyStaticLibrary.MyManagedObject`

So if your receiving warnings with this dot notation, its not looking in your module

[1]:
Reply

#13
The mistake I made was to change the name of the entity and the generated "NSManagedObject Subclass" and not updating the name of the class. How to fix:

- open .xcdatamodeld
- click on the entity
- open the right panel
- Go to Data Model Inspector
- Change the name of the Class text field
Reply

#14
it easy to answer to this question. without removed

@objc(Workout)

the solution is on documentary [Core Data Programming](

[To see links please register here]

) on "**Entity Name and Class Name**".
here in your Xcode before doing (**Editor** -> **Create NSManagedObject SubClass**)
must change your class Name of Entities to add "MO", CoreData can differentiate between class Name and Entitie Name. and the

@objc(Workout)

will not be created, give us this one:

class CDWorkoutMO: NSManagedObject {

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



[like I do on my Xcode][1]


[1]:
Reply

#15
[![enter image description here][1]][1]in your file modeldataClass
probably name of Class is incorrect cause before you change name something in your name class


[1]:
Reply

#16
In my case the same problem was in @objc modifier in auto-generated header of data-class

@objc(CachedMovie)
public class CachedMovie: NSManagedObject {

I just removed `@objc(CachedMovie)` and it began to work
Reply

#17
I was trying out adding CoreData to existing project and I renamed things a lot. I ended up with multiple .xcdatamodeld without knowing it. The solution was removing .xcdatamodeld and generated NSManagerObject and recreating it again.
Reply

#18
I came across this same error message when trying to insert/add a managed object from an extension (SiriKit). It seems to have been an issue with the namespace of the extension not matching the `.xcdatamodeld` file, because I was creating the entity description using `MyClass.entity()`.

The combination that worked for me was:

- `@objc(MyClass)` at the top of each `NSManagedObject` subclass
- Entities in the data model use the default "Global namespace", not "Current Product Module"
- Create the entity description using `let entity = NSEntityDescription.entity(forEntityName: "MyClass", in: context)!`
Reply

#19
I had similar issue in CoreData stack with `NSManagedObjectModel` made from `Swift` code. The issue was in **wrong value** for `NSEntityDescription.managedObjectClassName` attribute. The `Swift module` prefix was missed.

Correct setup:

let entity = NSEntityDescription()
entity.name = PostEntity.entityName // `PostEntity`
entity.managedObjectClassName = PostEntity.entityClassName // `MyFrameworkName.PostEntity`
entity.properties = [....]

Where: `entityName` and `entityClassName` defined like this.

extension NSManagedObject {

public static var entityName: String {
let className = NSStringFromClass(self) // As alternative can be used `self.description()` or `String(describing: self)`
let entityName = className.components(separatedBy: ".").last!
return entityName
}

public static var entityClassName: String {
let className = NSStringFromClass(self)
return className
}

}
Reply

#20
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")

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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