4

I have separated my Data Access Layer (DAL) in a framework in order to be able to use it in both the app and its Today widgets. I set up everything and it compiled but at runtime when I tried to create a NSPersistentContainer the app crashed with the infamous An NSManagedObject of class 'ClassName' must have a valid NSEntityDescription.

What I tried:

AXE
  • 8,335
  • 6
  • 25
  • 32

3 Answers3

4

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
AXE
  • 8,335
  • 6
  • 25
  • 32
1

I also faced the same issue while moving the data layer to a framework. The issue is NSPersistentContainer looks for an entity description in the main bundle by default, unless we create a custom subclass.

So the solution is to create a custom subclass of NSPersistentContainer in the embedded framework, and use it instead of the parent class. It should solve the problem.

0

In My case it was fixed when I changed

let container = NSPersistentContainer(name: "Your Project Name Here")

to

let container = NSPersistentContainer(name: "Your Data Model Name Here")

In Appdelegate.Swift

Ae Ri
  • 185
  • 1
  • 12