Is this the correct way to to use coredata with the new App protocol? My app works like this but im a beginner so dont know if this is making some bad performance or if the app will crash in future when the coredata model get more entrys?
See down:
ContentView is the navigation menu, and the views i navigate to from contentview can use the coredata through the @Environment of managedObjectContext now so it seems like all is working..
See down:
ContentView is the navigation menu, and the views i navigate to from contentview can use the coredata through the @Environment of managedObjectContext now so it seems like all is working..
Code Block swift import SwiftUI import CoreData @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext, persistentContainer.viewContext) } } var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "TestApp") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() 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)") } } } }