onmyway133 / notes

:notebook_with_decorative_cover: Issues and solutions I found during development, mostly iOS
https://onmyway133.com/
MIT License
62 stars 4 forks source link

BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered #568

Open onmyway133 opened 2 years ago

onmyway133 commented 2 years ago

[CK] BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered (com.apple.coredata.cloudkit.activity.export.2C51AA6A-6507-440F-8C96-7630A1604904).

Soundtwig commented 1 year ago

Got the same. Years later I am working on a SwiftUI app with Core Data and Swift Data used in parallell. I have a private and a shared store.

I had the modelContainer as @State var modelContainer = PersistenceController.shared.modelContainer in the @main App struct.

The solution seems to be just to use the static let persistenceController = PersistenceController.shared

This caused the error:

` @main struct MyApp: App {

@State private var modelContainer = PersistenceController.shared.modelContainer

var body: some Scene {
    WindowGroup {
        ContentView()
            .environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
            .modelContainer(modelContainer)
    }
}

} `

This works:

` @main struct MyApp: App {

let persistenceController = PersistenceController.shared

var body: some Scene {
    WindowGroup {
        ContentView()
            .environment(\.managedObjectContext, persistenceController.container.viewContext)
            .modelContainer(persistenceController.modelContainer)
    }
}

} `

berkaytheunicorn commented 11 months ago

Just happened to me.


final class ContainerContext: ObservableObject {
    @AppStorage("isCloudSyncEnabled")
    var isCloudSyncEnabled: Bool = false

    func isCloudSyncAvailable() -> Bool {
        if let _ = FileManager.default.ubiquityIdentityToken {
            return true
        } else {
            return false
        }
    }

    func setup() -> ModelContainer {
        print("ContainerContext setup.")
        print("ContainerContext isCloudSyncEnabled:", isCloudSyncEnabled)
        print("ContainerContext isCloudSyncAvailable:", isCloudSyncAvailable())
        do {
            let schema = Schema([
                Model.self,
                Model2.self,
            ])

            if isCloudSyncAvailable() {
                let config = ModelConfiguration(schema: schema, cloudKitDatabase: isCloudSyncEnabled ? .automatic : .none)

                return try ModelContainer(for: schema, configurations: config)
            } else {
                return try ModelContainer(for: schema)
            }
        } catch {
            fatalError("Failed to create model containers.")
        }
    }
}

and using like this

.modelContainer(container.setup())

but every time view is rendering it triggers the container.setup() and I guess this raises the error.