You need to create a custom Core Data class and inject it into ContentView()
I don‘t have a default file at hand but you can inject it like below
Code Block let context = PersistentCloudKitContainer.persistentContainer.viewContext |
|
ContentView().environment(\.managedObjectContext, context) |
Below my CloudKitContainer but you can simply change that to a regular PersistentContainer class
Code Block import CoreData |
|
public class PersistentCloudKitContainer { |
|
// MARK: - Define Constants / Variables |
public static var context: NSManagedObjectContext { |
return persistentContainer.viewContext |
} |
|
// MARK: - Initializer |
private init() {} |
|
// MARK: - Core Data stack |
public static var persistentContainer: NSPersistentCloudKitContainer = { |
|
let container = NSPersistentCloudKitContainer(name: "Container_Name") |
container.loadPersistentStores(completionHandler: { (storeDescription, error) in |
if let error = error as NSError? { |
|
fatalError("Unresolved error \(error), \(error.userInfo)") |
} |
}) |
|
// MARK: - Core Data Saving support |
public static func saveContext () { |
let context = persistentContainer.viewContext |
if context.hasChanges { |
do { |
try context.save() |
} catch { |
let nserror = error as NSError |
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") |
} |
} |
} |
} |