Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

Avoid Duplicate Records with CloudKit & CoreData
When my app starts it loads data (of vehicle models, manufacturers, ...) from JSON files into CoreData. This content is static. Some CoreData entities have fields that can be set by the user, for example an isFavorite boolean field. How do I tell CloudKit that my CoreData objects are 'static' and must not be duplicated on other devices (that will also load it from JSON files). In other words, how can I make sure that the CloudKit knows that the record created from JSON for vehicle model XYZ on one device is the same record that was created from JSON on any other device? I'm using NSPersistentCloudKitContainer.
3
0
3.3k
Jun ’25
Reply to No persistent stores error in SwiftData
The crash you’re seeing— FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation. (Apple Developer) —happens because by the time your onAppear runs, the debug block has already loaded and then removed the only store from the underlying NSPersistentCloudKitContainer. When you immediately call newContainer = try ModelContainer(for: Page.self, configurations: config) SwiftData’s internal coordinator finds no stores to save into and throws. The fix is to perform both the CloudKit‐schema initialization and your ModelContainer setup early, in your App struct (or in its init), rather than inside an onAppear. That way SwiftData creates its SQLite store after you’ve torn down the temporary CloudKit container, so it can recreate a fresh store. For example: @main struct MyApp: App { // 1) Build your debug store, initializeCloudKitSchema, remove it… // 2) Then immediately create the SwiftData ModelContainer private var mode
May ’25
No persistent stores error in SwiftData
I am following Apple's instruction to sync SwiftData with CloudKit. While initiating the ModelContainer, right after removing the store from Core Data, the error occurs: FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.; (user info absent) I've tried removing default.store and its related files/folders before creating the ModelContainer with FileManager but it does not resolve the issue. Isn't it supposed to create a new store when the ModelContainer is initialized? I don't understand why this error occurs. Error disappears when I comment out the #if DEBUG block. Code: import CoreData import SwiftData import SwiftUI struct InitView: View { @Binding var modelContainer: ModelContainer? @Binding var isReady: Bool @State private var loadingDots = @State private var timer: Timer? var body: some View { VStack(spacing: 16) { Text(Loading(loadingDots)) .font(.title2) .foregroundColor(.gray) } .padding() .onAppear { start
2
0
169
May ’25
CoreData Data Sharing with AppGroup
I have the following lines of code to access data through CoreData. import Foundation import CoreData import CloudKit class CoreDataManager { static let instance = CoreDataManager() let container: NSPersistentCloudKitContainer let context: NSManagedObjectContext init() { container = NSPersistentCloudKitContainer(name: ABC) container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { print(error.userInfo) } }) context = container.viewContext context.automaticallyMergesChangesFromParent = true context.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType) } func save() { do { try container.viewContext.save() print(Saved successfully) } catch { print(Error in saving data: (error.localizedDescription)) } } } I have confirmed that I can share data between iPhone and iPad. Now, I need to use AppGroup as well. I have changed my code as follows. import Foundation import CoreData import CloudKit class CoreDataManager { sta
1
0
115
May ’25
Toggle sync with NSPersistentCloudKitContainer
Is there a way to pause or disable syncing via NSPersistentCloudKitContainer at runtime? I want to give my users the option to enable or disable syncing, but there doesn't seem to be a good way to do it. I tried reloading the persistent stores and setting the store description's cloudKitContainerOptions to nil, but if I do that after first initialization, I get an error:error: Store opened without NSPersistentHistoryTrackingKey but previously had been opened with NSPersistentHistoryTrackingKey - Forcing into Read Only modeAnyone know of another way to do this?I submitted a request via Feedback Assistant for a flag to enable or disable syncing (FB6156182). I know the user could theoretically go into the Settings app and enable or disable iCloud access for the app as a whole, but that's not very discoverable, and disables all iCloud access. I'm looking for something a little more fine-grained (limited to the NSPersistentCloudKitContainer) and that I could control in my app's UI.
15
0
15k
Apr ’21
Reply to Removing NSPersistentHistoryTrackingKey causes error.
Example with NSPersistentHistoryTrackingKey: static let containerCloud: NSPersistentCloudKitContainer = { let description = NSPersistentStoreDescription() description.url = SELF.storeURL description.configuration = CloudKit description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: iCloud.jsblocker) let container = NSPersistentCloudKitContainer(name: Model) container.persistentStoreDescriptions = [description] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError(LoadPersistentStores() error (error), (error.userInfo)) } else { #if DEBUG print(DB container = CloudKit) #endif } }) return container }()
May ’25
NSPersistentCloudKitContainer losing data
Some users of my app are reporting total loss of data while using the app. This is happening specifically when they enable iCloud sync. I am doing following private func setupContainer(enableICloud: Bool) { container = NSPersistentCloudKitContainer(name: ) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy guard let description: NSPersistentStoreDescription = container.persistentStoreDescriptions.first else { fatalError() } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) if enableICloud == false { description.cloudKitContainerOptions = nil } container.loadPersistentStores { description, error in if let error { // Handle error } } } When user clicks on Toggle to enable/disable iCloud sync I just set the description.cloudKitContainerOptions to nil and then user is asked to restar
4
0
1.1k
Nov ’25
How is Record Zone Sharing done?
My use case is the following: Every user of my app can create as an owner a set of items. These items are private until the owner invites other users to share all of them as participant. The participants can modify the shared items and/or add other items. So, sharing is not done related to individual items, but to all items of an owner. I want to use CoreData & CloudKit to have local copies of private and shared items. To my understanding, CoreData & CloudKit puts all mirrored items in a special zone „com.apple.coredata.cloudkit.zone“. So, this zone should be shared, i.e. all items in it. In the video it is said that NSPersistentCloudKitContainer uses Record Zone Sharing optionally in contrast to hierarchically record sharing using a root record. But how is this done? Maybe I can declare zone „com.apple.coredata.cloudkit.zone“ as a shared zone?
2
0
1k
Apr ’25
Sync an interactive widget's Core Data store with the main app (and iCloud)
Hi everyone! I have an app on the App Store that uses Core Data as its data store. (It's called Count on Me: Tally Counter. Feel free to check it out.) One of the app's core feature is an interactive widget with a simple button. When the button is tapped, it's supposed to update the entity in the store. My requirement is that the changes are then reflected with minimal latency in the main app and – ideally – also on other devices of the same iCloud user. And vice-versa: When an entity is updated in the app (or on another device where the same iCloud user is logged in), the widget that shows this entity should also refresh to reflect the changes. I have read multiple articles, downloaded sample projects, searched Stackoverflow and the Apple developer forums, and tried to squeeze a solution out of AI, but couldn't figure out how to make this work reliably. So I tried to reduce the core problem to a minimal example project. It has two issues that I cannot resolve: When I update an entity in the app, the widget i
4
0
369
Apr ’25
Reply to Sync an interactive widget's Core Data store with the main app (and iCloud)
Showing a hint to users inside the widget that they should open the main app in order to sync is hardly a practical solution and destroys not only the user experience, but the very purpose of interactive widgets. If I need to open the app each time I've pressed a button on the widget, it's not very interactive after all. Yeah, your above argument is quite valid, and is why the technote mentions if that is an appropriate user experience. In this case, you might consider using CloudKit framework directly in your widget. That way, you manage the synchronization with your own code, without relying on NSPersistentCloudKitContainer. To read the data that is on the CloudKit server and is maintained by NSPersistentCloudKitContainer, see Reading CloudKit Records for Core Data. The data your widget writes to the CloudKit server using CloudKit APIs, assuming that it follows the rules described in the above article, should be able to synchronize with NSPersistentCloudKitContainer. This is more
Apr ’25
Reply to Sync an interactive widget's Core Data store with the main app (and iCloud)
Dear Ziqiao Chen, thank you for your reply and the hints pointing me in the right direction. I managed to solve my problem no. 1 and now the sync works both between the app and the widget, but also between different devices. I had already observed the .NSPersistentStoreRemoteChange notification in my sample project, but it turns out that wasn't even necessary when automaticallyMergesChangesFromParent is set to true on the viewContext. The Problem with Syncing from a Widget Now the problem is the following: Regarding using Core Data + CloudKit (NSPersistentCloudKitContainer) in an extension, I'd like to suggest against doing that ⬆️ Your reply and the linked Technical Note both imply that syncing data with iCloud this way from both the app and its widget is not reliable as two NSPersistentCloudKitContainers pointing to the same persistent store can get in conflict (running on different threads) and throw the following error: CloudKit setup failed because there is another instance of this pers
Mar ’25
Reply to Sync an interactive widget's Core Data store with the main app (and iCloud)
The reason your main app doesn't update when your widget changes the shared store is most likely because the viewContext in your main app doesn't automatically detect and merge remote changes. Here, remote changes mean the changes made by a different process, or by using batch processing. Your main app can detect and handle remote changes in the following way: Observe the the .NSPersistentStoreRemoteChange notification to get notified of any remote change. In the notification handler, consume the store persistent history to detect the relevant changes and merge them to the managed object context tied to your main app UI. Alternatively, you can manage to reset the context, and then re-fetch, which should give you the up-to-date data. For more information about this topic, see the following Apple sample projects: Loading and Displaying a Large Data Feed. Sharing Core Data objects between iCloud users. Regarding using Core Data + CloudKit (NSPersistentCloudKitContainer) in an extension, I'd like to sugg
Mar ’25
Reply to How to provide visual feedback about iCloud sync status when the user reinstalls an app?
Yeah, if you are using NSPersistentCloudKitContainer, NSPersistentCloudKitContainer.Event is the API that provides you the activity status. If you are using the CloudKit framework, a CloudKit operation triggers its completion handler, and so you can get the status from there. Note that the status you get from the mentioned APIs doesn't tell whether your local cache is synchronized with the server or not, because it is perfectly possible that a new change has been made on the server version immediately before you get notified that an activity / operation is completed. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Mar ’25
Reply to "BAD_REQUEST" in iCloudKit Telemetry
BAD_REQUEST typically means that the CloudKit server receives an invalid request. That can happen when, for example, your request has a CloudKit record or field that doesn't exist in the CloudKit schema. You mentioned your development scheme had been fully deployed to production, but that doesn't completely rule out the possibility of having invalid requests. For example, if you are using Core Data / SwiftData + CloudKit (NSPersistentCloudKitContainer), and the Core Data / SwiftData model in your app isn't completely mapped to the CloudKit schema, a bad request may happen. What CloudKit API are you using? For Core Data / SwiftData + CloudKit and the CloudKit framework, you might consider capturing and analyzing a sysdiagnose to hopefully find detailed messages that indicate the concrete reason of a bad request. If you are only using NSUbiquitousKeyValueStore, or using file system APIs to access iCloud Drive, you don't directly get involved to CloudKit requests, and so I’d suggest that you file a feed
Mar ’25