1

Since I use other extensions in my app, I want to define my CoreData and all the associated logic in a separate framework, which is then accessed by the main app, as well as all extensions.

When I want to access this from my main app I get the error message:

No NSEntityDescriptions in any model claim the NSManagedObject subclass 'FrameworkName.EntityName' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?

As the error message says, probably the Main app does not know the CoreData model, because it is not loaded there.

In other SO posts [1, 2] it is written that you have to make changes in AppDelegate. I added the following code hoping that this will load the CoreData, unfortunately it does not. Can anyone help.

@main struct WebListsApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    
    var body: some Scene {
        WindowGroup {
            FolderView()
        }
    } }

class AppDelegate: UIResponder, UIApplicationDelegate {

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "DataModelName")
        container.loadPersistentStores { description, error in
            if let error = error {
                fatalError("Unable to load persistent stores: \(error)")
            }
        }
        return container
    }()
    
    lazy var managedObjectModel: NSManagedObjectModel = {
        let frameworkBundleIdentifier = "frameworkIdentifier"
        let customKitBundle = Bundle(identifier: frameworkBundleIdentifier)!
        let modelURL = customKitBundle.url(forResource: "DataModelName", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()
}
fox
  • 41
  • 5
  • Have you tried cleaning the `build folder` & the `derived data`? – Timmy Aug 01 '22 at 12:45
  • Yes, I deleted the derived data and cleaned thee build folder. I also checkt the names of my DataModel and selected 'Current Product Module' as the Module in the DataModel for the Entities. These are the typical issues that I could think of, based on some other SO-posts. – fox Aug 01 '22 at 14:30
  • What code are you executing when you’re getting the error? – Timmy Aug 01 '22 at 16:24
  • I am running the main app (WebLists - code above) on a simulator. In the FolderView i just have a button, which calls a method from a class inside the framework with the CoreData. This method simply creates and adds a new Entity to CoreData. The Error above occurs, when I click the button in the FolderView in the simulator. When I run code using the CoreDataModel within the framework I do not get any errors. – fox Aug 01 '22 at 18:07

0 Answers0