Convert Coredata PersistenceController to SwiftData container

Hello

I have a CoreData PersistenceController and would like to convert it to a SwiftData container. Before converting the whole app to use SwiftData I wanted to know if my conversion is right and preserves old data (in iCloud and local).

Here is the code:

PersistenceController:


import CoreData
import SwiftUI
import Combine

#if os(iOS) || os(macOS) || os(watchOS)
import WidgetKit
#endif

struct PersistenceController {
    static let shared = PersistenceController()
    
    let container: NSPersistentCloudKitContainer
    
    init() {
        let fileContainer = URL.storeURL(for: "group.Water-Alert-App", databaseName: "CoreData")
        
        container = NSPersistentCloudKitContainer(name: "CoreData")
        let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL()
        
        let localStoreURL = defaultDirectoryURL.appendingPathComponent("Local.sqlite")
        let localStoreDescription = NSPersistentStoreDescription(url: localStoreURL)
        localStoreDescription.configuration = "Local"
        
        // Create a store description for a CloudKit-backed local store
        let cloudStoreDescription = NSPersistentStoreDescription(url: fileContainer)
        cloudStoreDescription.configuration = "Cloud"
        
        // Set the container options on the cloud store
        cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.namel")
        
        cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        
        container.persistentStoreDescriptions = [cloudStoreDescription, localStoreDescription]
        
        container.loadPersistentStores{ (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        }
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    }
    
    func save() {
        if container.viewContext.hasChanges {
            do {
                try container.viewContext.save()
            } catch {
                print("COULD NOT SAVE WITH CORE DATA")
            }
        }
    }
}

ModelContainer:


   var container: ModelContainer? = {
        let fileContainer = URL.storeURL(for: "group.Water-Alert-App", databaseName: "CoreData")

        let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL()
        
        let localSchema = Schema([Reminder.self])
        let cloudSchema = Schema([Goal.self, WaterData.self, Container.self])
        
        let localConfiguration = ModelConfiguration("Local", schema: localSchema, url: defaultDirectoryURL.appendingPathComponent("Local.sqlite"))

        let cloudConfiguration = ModelConfiguration("Cloud", schema: cloudSchema, url: fileContainer, cloudKitDatabase: .private("iCloud.Water-Alert-App-Offical"))
        
        let container = try! ModelContainer(for: Reminder.self, Goal.self, WaterData.self, Container.self, configurations: localConfiguration, cloudConfiguration)

        return container
    }()

Thank you!

Convert Coredata PersistenceController to SwiftData container
 
 
Q