Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

CoreData + CloudKit: Cocoa-Error 134419
Hi, Some of my users are experiencing synchronization issues when using the NSPersistentCloudKitContainer in CoreData. My app is listening to any errors that occur during the synchronization with the NSPersistentCloudKitContainer.eventChangedNotification Notification. Before I just got Cocoa-Errors 2 or 3 which indicate a bad network connection or something similar. Now I received reports with a Cocoa-Error 134419. Does anyone know what this error code means? Maybe someone from the Core Data team could have a look at this. Thanks a lot, Alexander
1
0
1.9k
Nov ’21
How to toggle CoreData CloudKit sync during App runtime
Hi, I'm currently developing a SwiftUI based app with Core Data and CloudKit sync with the NSPersistentCloudKitContainer. I found different solutions how to toggle CloudKit sync of the Core Data during runtime. The basic idea of these solutions is the following. instantiate a new NSPersistentCloudKitContainer set storeDescription.cloudKitContainerOptions = nil load persistence store Some solutions recommend to restart the app manually to avoid exactly my problem. Issues So far so good. How can I distribute the new viewContext through my app during runtime. In the main App I distributed the viewContext during startup via @Environment(.managedObjectContext) and it seems not be updated automatically after a reinitialization of NSPersistentCloudKitContainer. var body: some Scene { WindowGroup { ContentView() .environment(.managedObjectContext, persistence.container.viewContext) } } After deactivating the CloudKit sync I receive the following error when I try to add a new entity. [error]
5
0
2.7k
Nov ’21
What should be the expected user experience from an app that uses CloudKit/Sync
I have a SwiftUI app where I would like to enable NSPersistentCloudKitContainer to start using CloudKit and be able to sync the content of the app between multiple devices, but I'm not sure how to handle the user experience. Should this be a feature that the user turns On and Off in the app settings screen or does the user expects the syncing mechanism to work by default without having to do anything, can someone please share your experience on how you implemented CloudKit/Sync in your app. Do I need to offer a login mechanism so the user logs in? Should I include an On/Off option so the user decides if they want the sync option or not? In general, can someone be so kind and explain what the user experience should be when an app provides ClouldKit/sync? FYI - I'm not asking how to implement CloudKit sync I already know what it is and how it works. I just need to know what the expedition is from an app the provides that feature. Thanks!
1
0
740
Dec ’21
CloudKit: how to handle CKError partialFailure when using NSPersistentCloudKitContainer?
I'm using NSPersistentCloudKitContainer with Core Data and I receive errors because my iCloud space is full. The errors printed are the following: . I want to inform the user about this issue, but I can't find a way to access the details of the error. I'm listening to NSPersistentCloudKitContainer.eventChangedNotification, I receive a error of type .partialFailure. But when I want to access the underlying errors, the partialErrorsByItemID property on the error is nil. How can I access this Quota Exceeded error? import Foundation import CloudKit import Combine import CoreData class SyncMonitor { fileprivate var subscriptions = Set() init() { NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification) .sink { notification in if let cloudEvent = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey] as? NSPersistentCloudKitContainer.Event { guard let ckerror = cloudEvent.error as? CKError else { return } print(Error: (ckerror.localizedDes
2
0
1.4k
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
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
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
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
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
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
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
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
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
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
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