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:
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:
import SamplePackage
class ViewController: UIViewController {
override func viewDidLoad() {
					super.viewDidLoad()
	var container = SamplePackageDataStack.shared.persistentContainer
print(container)
		}
}
wwdc20-10169
RSS for tagDiscuss WWDC20 Session 10169 - Swift packages: Resources and localization
Posts under wwdc20-10169 tag
1 Post
Sort by: