Bizarre CoreData error when moving to data model to swift package

When moving my working Core Data model to a swift package, the package/app combo build but the app crashes on launch. The debugger points to the @main attribute on my App struct (using SwiftUI) and prints the following messages in the console:

"CoreData: error:  Failed to load model named StationData

Swift/UnsafeRawBufferPointer.swift:879: Fatal error: UnsafeRawBufferPointer with negative count

2022-03-09 20:46:29.156232-0600 Swift/UnsafeRawBufferPointer.swift:879: Fatal error: UnsafeRawBufferPointer with negative count"

I'm unsure as to what exactly is occurring here, especially since the model built and ran perfectly fine when it was a part of the app instead of the package.

Post not yet marked as solved Up vote post of thafner Down vote post of thafner
1.6k views

Accepted Reply

Changing the module to the swift package instead of the current project module worked for me.

Replies

It looks like your trying to load the model from the main bundle. You need to load it from the Swift package bundle and then pass it to the NSPersistentContainer initializer.

  • When using the managedObjectModel merged from the module's bundle, it says that it's confused because it says that no model has defined an entity relating to a particular subclass of NSManagedObject.

  • I have the same issue as @thafner—The fix "works", but comes with the new error they mention...

Add a Comment

Changing the module to the swift package instead of the current project module worked for me.