Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

Reply to Restart sync when using NSPersistentCloudKitContainer
With the following code I'm successfully turning On or Off sync between CoreData and CloudKit. I have iCloudSync saved in UserDefault / @AppStorage and controlled with a toggle switch in my app settings. This line is what turn it off description.cloudKitContainerOptions = nil, I hope it helps. class CoreDataManager: ObservableObject{ // Singleton static let instance = CoreDataManager() private let queue = DispatchQueue(label: CoreDataManagerQueue) @AppStorage(UserDefaults.Keys.iCloudSyncKey) private var iCloudSync = false lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }() func updateCloudKitContainer() { queue.sync { container = setupContainer() } } func setupContainer()->NSPersistentContainer{ let container = NSPersistentCloudKitContainer(name: YourCoreDataContainerName) guard let description = container.persistentStoreDescriptions.first else{ fatalError(###(#function): Failed to retrieve a persis
Jun ’24
Reply to CloudKit + SwiftData
In the CloudKit environment, duplicates can indeed happen if multiple peers can create the same data. We discuss this topic in Remove duplicate data. (As of today, SwiftData + CloudKit integration uses NSPersistentCloudKitContainer, and so the content applies.) Regarding CloudKit public database, yeah, it fits the use case where any user can generate data and all users can read the data. However, SwiftData + CloudKit public database integration is not supported today. You probably need to stick with NSPersistentCloudKitContainer if to use CloudKit public database. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24
Reply to SwiftData and CloudKit learning resource
In the SwiftData layer, the only thing you can use to configure CloudKit is ModelConfiguration.CloudKitDatabase. Underneath, SwiftData uses NSPersistentCloudKitContainer for CloudKit integration, so all the documentations and knowledges related to NSPersistentCloudKitContainer help understand how SwiftData + CloudKit works, and you can start with CloudKit mirroring. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24
Reply to CloudKit in TestFlight: No sync between devices 😭
In this situation, you might consider capturing and analyzing a sysdiagnose. The following technote discusses how to do so: TN3163: Understanding the synchronization of NSPersistentCloudKitContainer The following related technote may also help: TN3164: Debugging the synchronization of NSPersistentCloudKitContainer Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24
Reply to SwiftData Remote Database Updates
I am not quite clear what you meant by shared remote datastore. If you referred to SwiftData + CloudKit integration enabled with ModelConfiguration.CloudKitDatabase, it is based on NSPersistentCloudKitContainer, and doesn't use the DataStore API. If your intent is to detect the changes NSPersistentCloudKitContainer made on the store and make your fetched result set up to date, you can consider observing .NSPersistentStoreRemoteChange notification, and re-fetch the data in the notification handler. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24
Reply to SwiftData with shared and private containers
SwiftData + CloudKit public or shared database isn't supported today. If that is critical to your app, I suggest that you file a feedback report to voice your need. The framework, including the new DataStore API, doesn't provide supports in that regard. You can choose to implement your own way, similar to what NSPersistentCloudKitContainer does, but that will be obviously very involved. I'd probably stick with NSPersistentCloudKitContainer, if I do need the integration with CloudKit public or shared database. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24
What does "Allows External Storage" checkbox in the Attribute Inspector do?
For a bit of context, I store binary files of about 50KB in my Core Data. I noticed that enabling the Allows External Storage option doesn't seem to make any difference. Additionally, how does this setting work with CloudKit when using NSPersistentCloudKitContainer? Does this setting affect how the data is loaded into memory when accessed in Core Data in the app? Thank you very much!
1
0
691
Jun ’24
Reply to swiftUI apps with SwiftData and CloudKit crashes on iOS but works on MacOS
It seems to me that your code creates a persistent CloudKit container (NSPersistentCloudKitContainer) and a SwiftData model container (ModelContainer), and that both containers use the same model and manage the same store, which triggers a conflict. This is discussed in Avoid synchronizing a store with multiple persistent containers. Assuming that you have correctly configured CloudKit in your project, when you create a SwiftData model container with the default configuration (ModelConfiguration), SwiftData creates a NSPersistentCloudKitContainer under the hood, which automatically synchronizes the data store for you, and so you don't really need to create the persistent CloudKit container. Is there any special reason why you need a persistent CloudKit container and a SwiftData model container simultaneously? Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24
Reply to CloudKit sync refresh problem when change from iOS to macOS
Yeah, I see people reporting that CloudKit synchronization on macOS doesn't happen as quickly as it does on iOS, and that it only happens when the app comes to the foreground. I don't think that is really a bug because CloudKit was designed to synchronize only when it determines appropriate. In this case, it is that CloudKit on macOS chooses to synchronize when your app comes to the foreground. To be very clear, bringing your app to the foreground doesn't always trigger a synchronization, because a synchronization may be throttled if the rate is too high. For more information, see: TN3162: Understanding CloudKit throttles TN3163: Understanding the synchronization of NSPersistentCloudKitContainer In the production environment, that may not be a real issue, because people typically don't put their devices side by side to see the synchronization. Instead, they typically use your app on one device, and switch to another device some time later. When they switch the device and bring your app up, the synchr
Jun ’24
NSPersistentCloudKitContainer not syncing data on macOS
Hello, I am using NSPersistentCloudKitContainer to share data between iOS and macOS app.I have set up the project according to the official tutorials. And when I made changes from the macOS app, the lastest data will automatically display on my iOS app. But in the other direction when I made changes on iOS, macOS app will not get the lastest version of the data.I have verified on Cloud Dashboard that the changes made on iOS have been uploaded onto CloudKit server. Just the macOS app and the cloudd process won't create tasks to pull these changes (observed from console.app). And if I quit the macOS app and restart it, lastest data will be pulled.So the problem is that macOS using NSPersistentCloudKitContainer will not get synced data in the runtime.One potential reason is that the official tutorial has mentioned to set the background Remote Notification. I've done this for the iOS app. But for the macOS app, there are no background mode options so I skipped this step.Any ideas to get the sync
4
0
1.3k
Jun ’20
SwiftData+Cloudkit Migration Failed
I have v3 models in coredata (model: Event, Lecture), and make new model in v4 with swiftdata but model name, property is different (model: EventModel, LectureModel). For migration, I add V3Schema, V4Schema and MigrationPlan. enum V4Schema: VersionedSchema { static var models: [any PersistentModel.Type] = [LectureModel.self, EventModel.self ] static var versionIdentifier = Schema.Version(4, 0, 0) } enum V3Schema: VersionedSchema { static var models: [any PersistentModel.Type] = [Event.self, Lecture.self] static var versionIdentifier = Schema.Version(3, 5, 2) } enum ModelMigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] = [V3Schema.self, V4Schema.self] static var stages: [MigrationStage] = [migrateV3ToV4] } extension ModelMigrationPlan { static let migrateV3ToV4 = MigrationStage.custom(fromVersion: V3Schema.self, toVersion: V4Schema.self, willMigrate: willMigrate, didMigrate: { _ in Log.debug(message: Migration Complete) }) } private func willMigrate(context: ModelContext) thr
2
0
1.1k
May ’24
NSPersistentCloudKitContainer migration with custom NSEntityMigrationPolicy
Recently, I have made quite extensive changes to my schema and need to migrate my CoreData + CloudKit model to a new version. The changes require me to use a custom NSEntityMigrationPolicy because I have a rather complex mapping between some old entities and new entities. I have added a new model version. Deleted some entities, and added some entities. I have defined the NSEntityMigrationPolicy with createDestinationInstances(forSource:in:manager:) and createRelationships(forDestination:in:manager:). I have created a new Core Data Mapping Model. This was probably unnecessary. Within the Core Data Mapping Model, I have specified Custom Policy for all entities. In my container setup, I added two options, as below: storeDescription.setOption(false as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption) storeDescription.setOption(false as NSNumber, forKey: NSInferMappingModelAutomaticallyOption) I mostly followed a Core Data Heavyweight Migration guide. I'm unable to share the link, but it can be quite
1
0
695
May ’24
Can a private database entity have a relationship to a public database entity?
I’m using NSPersistentCloudKitContainer and I’m utilising the public database and also the user’s private database. For example I have an entity called Category which has a many-to-many relationship to an entity called NewsArticle. So the NewsArticles exist in the public database for the user to browse, but he can add them to a category which will live in his private database. So that’s my question, is it possible for an entity which exists a in the private database to have a relationship to another entity in a public database?
1
0
958
May ’24