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)!
}()
}