I am having issues loading my model from a Swift Package with the following structure:
| Package.swift
| Sources
| - | SamplePackage
| - | - Core
| - | - | - SamplePackageDataStack.swift
| - | - | - DataModel.xcdatamodeld
| - | - | - | - Model.xcdatamodel ( <- is this new? )
As mentioned, I am not required to list the xcdatamodeld as a resource in my Package manifest.
When trying to load the model in the main app, I am getting
CoreData: error: Failed to load model named DataModel
Code:
In my swift Package:
Main App:
| Package.swift
| Sources
| - | SamplePackage
| - | - Core
| - | - | - SamplePackageDataStack.swift
| - | - | - DataModel.xcdatamodeld
| - | - | - | - Model.xcdatamodel ( <- is this new? )
As mentioned, I am not required to list the xcdatamodeld as a resource in my Package manifest.
When trying to load the model in the main app, I am getting
CoreData: error: Failed to load model named DataModel
Code:
In my swift Package:
Code Block swift public class SamplePackageDataStack: NSObject { public static let shared = SamplePackageDataStack() private override init() {} public lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "DataModel") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() /// The managed object context associated with the main queue. (read-only) public var context: NSManagedObjectContext { return self.persistentContainer.viewContext } public func saveContext () { if context.hasChanges { do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } } }
Main App:
Code Block swift import SamplePackage class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var container = SamplePackageDataStack.shared.persistentContainer print(container) } }