It seems that AppDelegate and SceneDelegate are no longer needed in Swift UI in Xcode 12. I'm having trouble figuring out how to build an iCloud/Core Data enabled application with Swift UI. Is there a video/tutorial/guide on implementing Core Data or Cloudkit with the new Swift UI frameworks yet?
You can create your custom PersistentCloudKitContainer Class.
One thing I was still struggling with was to apply the Merge Policies. I couldn't figure out how this works. Thats why they are commented out in my code below.
And my Custom Class
One thing I was still struggling with was to apply the Merge Policies. I couldn't figure out how this works. Thats why they are commented out in my code below.
Code Block import SwiftUI import CoreData @main struct TestApp: App { let context = PersistentCloudKitContainer.persistentContainer.viewContext //context.automaticallyMergesChangesFromParent = true //context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy var body: some Scene { WindowGroup { ContentView().environment(\.managedObjectContext, context) } } }
And my Custom 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: NSPersistentContainer = { let container = NSPersistentContainer(name: "Model") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() // 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)") } } } }