Hi,
Thanks for your response. I don't think we do any of the things you are concerned about.
Here is some of the code I use to set up the CoreData/Cloudkit stack and have been using this for a few years now. Nothing changed here in at least 12 months (I am not exactly a piker in this realm :) :
class CoreDataUtil: NSObject
{
@objc var pc = NSPersistentCloudKitContainer(name:"iphemeris")
private lazy var historyRequestQueue = DispatchQueue(label:"history")
let coreDataLogSubsystem = "com.iPhemeris.coreData"
let coreDataLogger = Logger(subsystem:"com.iPhemeris.coreData", category:"Cloudkit")
@objc var useiCloud:Bool = userSettings.bool(forKey:UseIcloudKey) {
didSet {
userSettings.set(useiCloud, forKey:UseIcloudKey)
if(pc.persistentStoreCoordinator.persistentStores.count > 0)
{
let store = pc.persistentStoreCoordinator.persistentStores[0]
do {
try pc.persistentStoreCoordinator.remove(store)
}
catch {
coreDataLogger.log("*** CoreDataUtil toggle use iCLoud ON/OFF FAILED: \(error, privacy:.public)")
}
}
self.initCoreDataStack()
}
}
@objc func initCoreDataStack()
{
coreDataLogger.log("*** Initialize CoreData Stack - Start")
guard let description = pc.persistentStoreDescriptions.first else {
fatalError("*** CoreDataUtil - could not find persistent store description.")
}
description.setOption(true as NSNumber, forKey:NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey)
description.setOption(true as NSNumber, forKey:NSMigratePersistentStoresAutomaticallyOption)
description.setOption(true as NSNumber, forKey:NSInferMappingModelAutomaticallyOption)
if(useiCloud == true) {
coreDataLogger.log("*** User is using iCloud")
let cloudKitContainerIdentifier = "iCloud.cribaudo.iphemeris"
let options = NSPersistentCloudKitContainerOptions(containerIdentifier:cloudKitContainerIdentifier)
description.cloudKitContainerOptions = options
}
else {
coreDataLogger.log("*** User is NOT using iCloud")
description.cloudKitContainerOptions = nil
}
pc.persistentStoreDescriptions.first?.url = storeURL()
pc.viewContext.automaticallyMergesChangesFromParent = true
pc.loadPersistentStores { _, error in
if let error = error as NSError? {
self.coreDataLogger.error("*** loadPersistentStore ERROR: \(error, privacy:.public), \(error.userInfo, privacy:.public)")
}
#if DEBUG
// Can be called every time but is needed after model changes
//self.initializeCloudKitSchemaForNewModel()
#endif
self.pc.viewContext.automaticallyMergesChangesFromParent = true
// De-dupe old records out of Database.
let uuidFixed = NSUbiquitousKeyValueStore.default.object(forKey:CoreDataUUIDNullAndDuplicatesFixed)
if(uuidFixed == nil) {
self.coreDataLogger.log("*** User does not have uuidFixed Set.")
// OLD DeDupe Code - probably can delete soon.
// Run UUID De-dupe if it was not yet run for this users account
self.findAndfixDuplicateUUID()
}
else {
self.coreDataLogger.log("*** User has uuidFixed Set.")
self.deDupeMigratedData()
self.findAndfixMissingFirstChar()
}
// Notify everyone the data is ready
self.sendMOCReadyNotificationForPlatform()
self.coreDataLogger.log("*** Current CoreData Model Version -> \(self.pc.managedObjectModel.versionIdentifiers.description, privacy:.public)")
//self.listAllChartUUID()
}
// Try to get some error notification of iCloud Storage quota exceeded?
NotificationCenter.default.addObserver(self, selector:#selector(processCloudKitErrors), name:NSPersistentCloudKitContainer.eventChangedNotification, object:nil)
coreDataLogger.log("*** Initialize CoreData Stack - End")
}
func initializeCloudKitSchemaForNewModel()
{
coreDataLogger.log("*** Initialize CloudKit Scheme for New Model - Start")
if(!useiCloud) { return }
do {
// Use the container to initialize the development schema.
let options = NSPersistentCloudKitContainerSchemaInitializationOptions()
try pc.initializeCloudKitSchema(options:options)
}
catch {
coreDataLogger.error("*** CoreDataUtil Initializing new schema: \(error, privacy:.public)")
}
coreDataLogger.log("*** Initialize CloudKit Scheme for New Model - End")
}
func storeOptions() -> [AnyHashable:Any]
{
return [
NSMigratePersistentStoresAutomaticallyOption:true,
NSInferMappingModelAutomaticallyOption:true
]
}
func storeURL() -> URL
{
let urls = FileManager.default.urls(for:.documentDirectory, in:.userDomainMask)
guard let docURL = urls.last else {
fatalError("*** CoreDataUtil RW: StoreURL Error fetching document directory")
}
let storeURL = docURL.appendingPathComponent("iPhemeris.sqlite")
return storeURL
}
sdds