I have a SwiftUI app using Core Data and CloudKit with the NSPersistentCloudKitContainer
. I pass the NSManagedObjectContext
to my views with an environment variable like so:
WindowGroup {
HomePage()
.environment(\.managedObjectContext, DataManager.instance.context)
}
I have a toggle for tuning iCloud sync on and off, which generates the new NSPersistentCloudKitContainer
, which in turn creates a new context. However, all the fetched data before with the @FetchRequest
still uses the previous context from the environment object, hence manipulating it is no longer possible and I get errors.
I need to pass the new context to my views, so that @FetchRequest
would use it instead. How can I do that?