Migrating iOS 13 app to 14: how to manage AppDelegate context code for CoreData?

I'm in the middle of updating my app to only support iOS 14+ and am moving to the new @main app structure. I'm trying to figure out how to deal with code in my App and Scene Delegate: mainly code related to Core Data and CloudKit persistent container.

Stuff like

Code Block lazy var persistentContainer: NSPersistentCloudKitContainer = {...}


and

Code Block  let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContextcontext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy context.automaticallyMergesChangesFromParent = true 


Where do I put this kind of stuff in the new @main world?





You initialise the persistentContainer in the init method.

Code Block swiftstruct YourApp: App {    var persistentContainer: NSPersistentContainer    init() {        let container = NSPersistentContainer(name: "YourApp")        container.loadPersistentStores(completionHandler: { (storeDescription, error) in            if let error = error as NSError? {                fatalError("Unresolved error \(error), \(error.userInfo)")            }        })        self.persistentContainer = container    }    var body: some Scene {        WindowGroup {            ContentView()                .environment(\.managedObjectContext, persistentContainer.viewContext)        }    }}


Migrating iOS 13 app to 14: how to manage AppDelegate context code for CoreData?
 
 
Q