I faced the same issue. I was able to solve it by modifying my NSPersistantContainer as follows:
let container: NSPersistentContainer = {
let pc = NSPersistentContainer(name: "Model")
let storeURL = URL.storeURL(for: "group.hourglass", databaseName: "group.hourglass")
let storeDescription = NSPersistentStoreDescription(url: storeURL)
pc.persistentStoreDescriptions = [storeDescription]
pc.loadPersistentStores { _, error in
if let error = error {
fatalError(error.localizedDescription)
}
}
return pc
}()
using this extension on URL:
public extension URL {
static func storeURL(for appGroup: String, databaseName: String) -> URL {
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
fatalError("Shared file container could not be created.")
}
return fileContainer.appendingPathComponent("\(databaseName).sqlite")
}
}
where group.hourglass is the name of my shared App Group between my App and Widget targets.
Source: SwiftLee article: "Core Data and App extensions: Sharing a single database"