Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

Reply to Avoid Duplicate Records with CloudKit & CoreData
NSPersistentCloudKitContainer doesn’t support unique constraints today. To make sure that a record is unique across devices, you need to implement your own mechanism. In case you are interested in doing that, I have a sample project (I use it as my testbed) that may help: Sharing Core Data Objects Between iCloud Users It is a SwiftUI project that supports iOS and watchOS, and covers the following topics: Creating the CloudKit Schema for Apps Setting up the Core Data Stack Sharing a Core Data object Detecting Relevant Changes by Consuming Store Persistent History Removing Duplicate Data Implementing a Custom Sharing Flow #4 and #5 are probably the most relevant to your question. Feel free to provide your feedback here so I can improve the project and hopefully better help.
Feb ’22
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
Feb ’22
Cloudkit - Coredata and multiples database
Today, I'm working without cloudkit (all data are in a local Coredata), with one database on my Xcode project but with 2 containers on app.delegate : one for a test environment : with storeURL ---> test.sqlite one for a production environment : with storeURL --> production.sqlite Everything is ok, the test data, is really separate from production data. Now, I try to use cloudkit functions : I change NSPersistentContainer to NSPersistentCloudKitContainer it's working and I could use data on 2 iPads. BUT if I switch the test to the prod, now the data from test environnement is duplicate on the production environnement. what mistake am I making? How could I use my two storeURL with cloudkit coredata? Thanx for your help.
2
0
723
Jan ’22
Reply to Siri Shortcuts with SwiftUI and Core Data
Short Answer: It is a bug. Just run (command + R) your app then stop it. Then talk to your Siri. You will see the new Item is added to your list. I think debugger stops core data to be updated from external events. Long Answer: I also tried many different approaches in the web and nothing worked. But here is the simplest code that I could add item to my list via Siri. Simply setup your PersistenceController public struct PersistenceController { public static let shared = PersistenceController() public let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: YourApp.) if inMemory { container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: /dev/null) } else { let storeURL = AppGroup.coredata.containerURL.appendingPathComponent(YourApp.sqlite) let description = NSPersistentStoreDescription(url: storeURL) container.persistentStoreDescriptions = [description] } container.loadPersistentStores(completionHandler: { (sto
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
On iOS 15.2, how do I load a cloudKit public container with user not logged in to iCloud
I am using the following to initialize my PersistenCloudKitContainer with the goal of loading a shared poblic cloudKit database: container = NSPersistentCloudKitContainer(name: FaceExplorer) guard let description = container.persistentStoreDescriptions.first else { print(Can't set description) fatalError(Error) } //ios 14 bug fix https://developer.apple.com/forums/thread/682925 description.cloudKitContainerOptions?.setValue(1, forKey: databaseScope) // normal / ios 15 version description.cloudKitContainerOptions?.databaseScope = .public //seems to work & Load on iOS14.5 description.cloudKitContainerOptions?.setValue(iCloud.com.myDomain.myContainerID, forKey: containerIdentifier) container.loadPersistentStores(completionHandler: { (storeDescription, error) in ... This all works great on iOS14 and also loads on iOS15.0 - 15.2 with users logged into iCloud with the iCloud drive option checked. However, when the iCloud drive option is not checked on iOS15 only, the container will not load. Please hel
0
0
589
Jan ’22
Filter store transactions Core Data
Hello I have this Core Data stack and I have an observer to observe NSPersistentStoreRemoteChange, I would like to filter changes that the user has made to call the mergePersistentHistoryChanges() just when needed, I think it has to be done in the fetchPersistentHistoryTransactionsAndChanges() function but I don't know how to do it. Can you help me. Thank You Here is my Core Data Stack: class PersistenceController { static let shared = PersistenceController() private var notificationToken: NSObjectProtocol? init() { notificationToken = NotificationCenter.default.addObserver(forName: .NSPersistentStoreRemoteChange, object: nil, queue: nil) { note in Task { await self.fetchPersistentHistory() } } } deinit { if let observer = notificationToken { NotificationCenter.default.removeObserver(observer) } } private var lastToken: NSPersistentHistoryToken? /// A persistent container to set up the Core Data stack. lazy var container: NSPersistentCloudKitContainer = { let fileContainer = URL.storeURL(for: group n
0
0
968
Jan ’22
Reload NSPersistentCloudKitContainer from a SwiftUI View - MVVM
How can I reload the NSPersistentCloudKitContainer from a SwiftUI View? I have a SwiftUI + MVVM + CloudKit app that successfully syncs to CloudKit but what I would like to be able to do is reload the NSPersistentCloudKitContainer from some View in the app to be able to evaluate if the app should sync to CloudKit or not by setting the cloudKitContainerOptions to nil (description.cloudKitContainerOptions = nil) if the user doesn't want to sync. In other words, I need to reload the code inside the init() method in the CoreDataManager file when a method in the View Model is called. See the code and comments below. Here is the code... Core Data Manager class CoreDataManager{ static let instance = CoreDataManager() let container: NSPersistentCloudKitContainer let context: NSManagedObjectContext init(){ container = NSPersistentCloudKitContainer(name: CoreDataContainer) guard let description = container.persistentStoreDescriptions.first else{ fatalError(###(#function): Failed to re
1
0
790
Jan ’22
Old local persistent store to new CloudKit store- lightweight migration
I have a new model which syncs to cloudkit. I'have to migrate my old core data store which is local to the new model which will store in cloudkit. I'm not sure how to perform this migration. The container is initialised correctly and the Mapping Model is mapping my entities and attributes correctly. There are no errors. Yet, I do not see my old records upon fetching. I found this lines of code here... NSDictionary *optionsDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, <#Ubiquitous content name#>, NSPersistentStoreUbiquitousContentNameKey, nil]; However, I'm not sure where to add it. Since, I had created a new project folder with core data + CloudKit (and imported the old model), this is the method i got in AppDelegate which initialises the store... - (NSPersistentCloudKitContainer *)persistentContainer { // The persistent container for t
5
0
754
Dec ’21
Fetch data from CloudKit to app when app is in background
Hello I have an app that uses Core Data with CloudKit to store data. When I am in the app (using the app) and create some new data on an other device, the data is fetched in a few seconds and I see it immediately, however if I am not using the app and create some new data on an other device, I have to enter the app and then the data starts fetching, is there a way to fetch data even if I am not using the app. Here is my core data stack: import CoreData import Combine class PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init() { container = NSPersistentCloudKitContainer(name: CoreData) guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: APP_GROUP_NAME)?.appendingPathComponent(CoreData.sqlite) else { fatalError(Shared file container could not be created.) } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSP
1
0
1.8k
Dec ’21
How to turn On/Off iCloudKitSync on a SwiftUI application
I'm trying to give the user the ability to decide whether they want to sync to CloudKit or not by turning On or Off a Switch located somewhere in the app settings screen but I'm not sure how to do it in SwiftUI. The following code successfully stops the sync to CloudKit by setting the cloud kit container options to nil description.cloudKitContainerOptions = nil. class CoreDataManager{ static let instance = CoreDataManager() let container: NSPersistentCloudKitContainer let context: NSManagedObjectContext init(){ container = NSPersistentCloudKitContainer(name: CoreDataContainer) guard let description = container.persistentStoreDescriptions.first else{ fatalError(###(#function): Failed to retrieve a persistent store description.) } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.cloudKitContainerOptions = nil container.loadPersistentStores { (descri
2
0
1.5k
Dec ’21
Share RecordZones or ParentRecords in CoreData-CloudKit Share
Hello everyone, in the App I'm building I'd like to share an entire list of items (records) by just sharing the list itself (parent record or RecordZone). Meanwhile, I'd like to continue using the NSPersistentCloudKitContainer and therefore CoreData sync. Is it possible, with the new features Apple introduced (Sharing RecordZone and Sharing in Cloud & Local Storage), to do what I described, and combine both functions. Thank you, Carlos Steiner
0
0
763
Dec ’21
NSPersistentCloudKitContainer - mirror sync
My app uses a mirrored CoreData Store with NSPersistentCloudkitContainer. The user has the option to turn cloudKit sync on or off by device. The app responds by setting the description?.cloudKitContainerOptions = nil - but keeps historyTracking on by setting description?.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey) The app is working fine this way with multiple devices and if the user re-enables sync on a particular device that was either disabled or offline everything syncs just fine on all devices. I now want to prune History transactions and I intend to clear the history older than say 7 days only when I receive a NSPersistentStoreRemoteChangeNotificationPostOptionKey observation on transactions that are outside the app. Does a device that was either offline or had sync disabled as per above then re-enabled or come back online upload its local changes first then download changes from the store? I am concerned that if is not this way I will potentially have to keep all History
0
0
710
Dec ’21
Is it possible to unload/change the PersistenceStore in a SwiftUI App during runtime
Hi, I want to activate/deactivate the CloudKit Sync during App runtime in a user settings view. Basically this works fine. Every time I toggle between the NSPersistentContainer and the NSPersistentCloudKitContainer, I increase the persistence.persistenceContainerReloaded attribute and the whole view hierarchy will be reloaded. Thus all changes are passed through the whole app. During the reload phase I have to load a new persistence store by container.loadPersistentStores(...). Unfortunately, I cannot remove the old persistence store before loading the new one. The app crashes immediately, because the store and viewContext is still in use. Therefore, I just create a new one and trigger the reload. Afterwards every view is using the new viewContext. But somewhere in the background there is still the old persistence store with CloudKit Sync active and pushes every local change to the cloud. Changes on the cloud from other devices are not received anymore. Does someone has any idea, how to correctly unl
6
0
1.3k
Dec ’21
Reply to PersistenceController and CloudKit
struct PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init() { container = NSPersistentCloudKitContainer(name: MJ) guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group.com.Jad.MJ)?.appendingPathComponent(MJ.sqlite) else { fatalError(Shared file container could not be created.) } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: YOUR containerIdentifier) container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError(Unresolved error (error), (er
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21