Hi,
I'm developing an app for iOS and MacOS with SwiftData and CloudKit syncing. I had sync working very well with a set of models. This schema was also pushed to CloudKit production.
Last week I added several models, and several relationship properties linking my existing models to newly added models. The schema updated just fine and everything worked.
Then things went sideways: earlier this week I decided I wanted to rename a property on one of my new models. So I renamed the property, removed the application support folder for my local debug app (on Mac OS), removed the app from the iOS Simulator (to clear its local database), and finally reset my CloudKit container to its Production schema. Basically, I tried to go back to the same state I had as when I first added the new models.
However, this time things don't go so smoothly, even after starting the app several times, rebooting my machine, turning iCloud on and off in Xcode and MacOS and iOS. When I look in CloudKit console, I see only my old models there: none of the new ones are added.
I'd love some pointers on how I can best debug this issue, as I feel completely stuck.
On MacOS
I have very little
mac-logs.txt
to go on. Since the logs are a bit lengthy I've added them as an attachment. I get a few warnings, but it is unclear what they are warning me about.
One thing that does stand out is that I am running the CloudKit in Development mode here. However, the logs do state accountPartition=Prod . And when I query CKContainer.default() for the container environment, the response is sandbox, which matches Development!
On iOS
The logs show a few errors, but I cannot make sense of them.
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1240): <NSCloudKitMirroringDelegate: 0x600003d09860>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x103325c90> (URL: file:///Users/bastiaan/Library/Developer/CoreSimulator/Devices/BF847CE5-A3E2-4B4C-8CD5-616B75B29AFE/data/Containers/Data/Application/0A916F67-B9B2-457B-8FA7-8C42819EA9AA/Library/Application%20Support/default.store)
<CKError 0x600000c433f0: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2310): <NSCloudKitMirroringDelegate: 0x600003d09860> - Attempting recovery from error: <CKError 0x600000c433f0: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:]_block_invoke(2773): <NSCloudKitMirroringDelegate: 0x600003d09860>: Found unknown error as part of a partial failure: <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:](2820): <NSCloudKitMirroringDelegate: 0x600003d09860>: Error recovery failed because the following fatal errors were found: {
"<CKRecordZoneID: 0x600000c8fd50; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>" = "<CKError 0x600000c956b0: \"Internal Error\" (1/5005); \"Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>\">";
}
And in CloudKit logs I see:
06/11/2024, 9:09:59 UTC 738513AC-9326-42DE-B4E2-DA51F6462943 iOS;18.1 ZoneFetch EphemeralGroup
{
"time":"06/11/2024, 9:09:59 UTC"
"database":"PRIVATE"
"zone":"com.apple.coredata.cloudkit.zone"
"userId":"_0d9445f850459ec351330ca0fde4134f"
"operationId":"611BA98C9B10D3F2"
"operationGroupName":"EphemeralGroup"
"operationType":"ZoneFetch"
"platform":"iPhone"
"clientOS":"iOS;18.1"
"overallStatus":"USER_ERROR"
"error":"ZONE_NOT_FOUND"
"requestId":"738513AC-9326-42DE-B4E2-DA51F6462943"
"executionTimeMs":"53"
"interfaceType":"NATIVE"
}
Any pointers are greatly appreciated!
Bastiaan
iCloud & Data
RSS for tagLearn how to integrate your app with iCloud and data frameworks for effective data storage
Post
Replies
Boosts
Views
Activity
I'm continually getting an error with a new CloudKit container when I try to save data.
error: Couldn't get container configuration from the server for container "iCloud.com.***.***"
here's the class:
private var db = CKContainer(identifier: "iCloud.com.***.***").privateCloudDatabase
func addTask(taskItem: TaskItem) async throws {
checkStatus()
do {
try await db.save(taskItem.record)
} catch {
print("error: \(error.localizedDescription)")
}
}
func checkStatus() {
let id = CKContainer(identifier: "iCloud.com.***.***").containerIdentifier
print(id ?? "unknown")
Task {
let status = try await CKContainer(identifier: "iCloud.com.***.***").accountStatus()
switch status {
case .available:
print("available")
case .noAccount:
print("no account")
case .restricted:
print("restricted")
case .couldNotDetermine:
print("could not determine")
case .temporarilyUnavailable:
print("temporarily unavailable")
@unknown default: break
}
}
}
The account status reports as available but gives the error on an attempt to save.. I'm trying to work out what I might be doing wrong..
Is it possible to use CloudKit and add integrations, Google Drive for example. If possible, how?
I've developed an app that contains an inbox that displays message from a CloudKit container. Works perfectly on simulator. Once I tried to run it on a phone..in Xcode debug environment and TestFlight it is unable to complete any transactions with production database. I'm running out of ideas. So far I have tried:
Verify settings between debug and release in Signing & Capabilities
Add CloudKit.framework to Framework, Libraries, and Embedded Content
Verify record and key names
verify .entitlements files
Please help!
I have Core Data setup with a NSPersistentCloudKitContainer as my container, I've added a container identifier and I see my data on CloudKit's database here when I use "Act As iCloud Account". Here's what I have under capabilities in Xcode
However, on my device when I go to Settings > Apple Account > iCloud > Saved to iCloud and switch off my app, all the data saved to Core Data is removed. I suspected this working as intended. When I switch it back on, all the data comes back. However (x2), the support page here says it should remain on the device:
When you turn it off, the app no longer connects with iCloud, so your data exists only on your device
Am I missing something in how I integrated Core Data in Xcode? Do I need to explicitly configure something with Apple's SDK to get the behavior described in the support page?
I've noticed for some of Apple's apps, when you switch off iCloud there's an action sheet asking what you'd like to do with the local data. I figured this was Apple's magic without sharing especially since the buttons looked different
Stocks
Safari
Contacts
However (x3), not all apps that had this option offered "Keep on My iPhone", so perhaps the supported behavior is to remove what's on the device and these Apple apps implemented their own support to keep a copy on the device.
Reminders
I've tried testing some 3rd-party apps but couldn't convince myself they were using Core Data with iCloud enabled. Instead, it looked like they were using iCloud as a backup
Ive been getting this error on an app in the dev environment since iOS16. it continues to happen in the latest iOS release (iOS18).
After this error/warning, CoreData_CloudKit stops syncing and the only way to fix it is to delete the app from all devices, reset the CloudKit dev environment, reload the schema and reload all data. im afriad that if I ever go live and get this error in production there won't be a way to fix it given I cant go and reset the production CloudKit environment.
It doesn't happen straight away after launching my app in a predictable manner, it can take several weeks to happen.
Ive posted about this before here and haven't got a response. I also have a feedback assistant issue submitted in 2022 as part of ios16 beta that is still open: FB10392936 for a similar issue that caused the same error.
would like to submit a code level support query but it doest seem to have anything to do with my code - but rather the Apple core data CloudKit syncing mechanism.
anyone have any similar issues or a way forward?
> error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _requestAbortedNotInitialized:](2200): <NSCloudKitMirroringDelegate: 0x301e884b0> - Never successfully initialized and cannot execute request '<NSCloudKitMirroringImportRequest: 0x3006f5a90> D823EEE6-EFAE-4AF7-AFED-4C9BA708703B' due to error: Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0x53, 0x6f, 0x6d, 0x65, 0x20, 0x73, 0x61, 0x6d)" UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0x53, 0x6f, 0x6d, 0x65, 0x20, 0x73, 0x61, 0x6d)}
Hello, I`m working on an app that uses CloudKit and CKShare, but the app has 2 different targets, one for professional and one for patients, and theoretically the target of the professional sends the CKShare and the target of the patient should accept, but the ckshare tries to always open the target of the profissional, I would like to know if the are any way to configure the CKShare to oppen the target od the patients
I have a few apps that use Core Data & CloudKit to sync data to multiple devices. Everything was fine until I updated to Xcode 14. Now, although the apps work on an actual device, in the simulator, I get errors about "Failed to sync user keys" and it won't sync anything.
I haven't changed the code at all. It just suddenly won't work in the simulator. Since it does this for all apps, I have to believe it's something changed with the simulator?
2022-09-13 12:47:26.766223-0400 Time Since[8061:88974] [error] error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1134): <NSCloudKitMirroringDelegate: 0x600003b540e0>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x1219071d0> (URL: file:///Users/jon/Library/Developer/CoreSimulator/Devices/1990772E-CDF1-4A58-B454-E09D327B2182/data/Containers/Data/Application/3314EA95-4D86-4053-84B3-F1523A57E204/Library/Application%20Support/TimeSince.sqlite)
<CKError 0x600000cc1e60: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600000cc2160: "Internal Error" (1/5000); "Failed to sync user keys">
}>
I've just tried to update a project that uses SwiftData to Swift 6 using Xcode 16 beta 1, and it's not working due to missing Sendable conformance on a couple of types (MigrationStage and Schema.Version):
struct LocationsMigrationPlan: SchemaMigrationPlan {
static let schemas: [VersionedSchema.Type] = [LocationsVersionedSchema.self]
static let stages: [MigrationStage] = []
}
struct LocationsVersionedSchema: VersionedSchema {
static let models: [any PersistentModel.Type] = [
Location.self
]
static let versionIdentifier = Schema.Version(1, 0, 0)
}
This code results in the following errors:
error: static property 'stages' is not concurrency-safe because non-'Sendable' type '[MigrationStage]' may have shared mutable state
static let stages: [MigrationStage] = []
^
error: static property 'versionIdentifier' is not concurrency-safe because non-'Sendable' type 'Schema.Version' may have shared mutable state
static let versionIdentifier = Schema.Version(1, 0, 0)
^
Am I missing something, or is this a bug in the current seed? I've filed this as FB13862584.
While reading CkSyncEngine demo project code, I don't find the code to remove items in syncEngine.state.pendingRecordZoneChanges explicitly. I suspect it might occur in two possible places: nextRecordZoneChangeBatch() or ``nextRecordZoneChangeBatch()`, but I can't figure out how it occurs.
nextRecordZoneChangeBatch() has the following code:
let batch = await CKSyncEngine.RecordZoneChangeBatch(pendingChanges: changes) { recordID in
if let contact = contacts[recordID.recordName] {
let record = contact.lastKnownRecord ?? CKRecord(recordType: Contact.recordType, recordID: recordID)
contact.populateRecord(record)
return record
} else {
// We might have pending changes that no longer exist in our database. We can remove those from the state.
syncEngine.state.remove(pendingRecordZoneChanges: [ .saveRecord(recordID) ])
return nil
}
}
(I'll ignore the syncEngine.state.remove(pendingRecordZoneChanges:) in the else clause, because it's unrelated)
Could it be that CKSyncEngine.RecordZoneChangeBatch.init(pendingChanges:,recordProvider:) automatically remove a CKRecord when the recordProvider: closure returns a non-nil value? I checked its document, but it doesn't say anything about this.
Thanks for any help.
I have a quesiton on .accountChange handler code in CKSyncEngine demo project. Below is the code in handleAccountChange():
if shouldDeleteLocalData {
try? self.deleteLocalData() // This error should be handled, but we'll skip that for brevity in this sample app.
}
if shouldReUploadLocalData {
let recordZoneChanges: [CKSyncEngine.PendingRecordZoneChange] = self.appData.contacts.values.map { .saveRecord($0.recordID) }
self.syncEngine.state.add(pendingDatabaseChanges: [ .saveZone(CKRecordZone(zoneName: Contact.zoneName)) ])
self.syncEngine.state.add(pendingRecordZoneChanges: recordZoneChanges)
}
IMHO, when user switches account, the most important thing is to reload data from the new account's document folder. However, I can't see this is done anywhere. In above code, if shouldDeleteLocalData is false, self.appData would still hold the previous account's local data. That seems very wrong. Am I missing something?
It would be best if iOS restarts all applications when user switches account. If that's not the case (I guess so, otherwise there is no point to handle .accountChange in the app), I think application should implement an API to re-initialize itself.
EDIT: after looking at the code again, I realize that the following code makes sure shouldDeleteLocalData is always true when user switching accounts. So the code doesn't leak the previous account's data, though I still think it has an issue - it doesn't load the new account's data.
case .switchAccounts:
shouldDeleteLocalData = true
shouldReUploadLocalData = false
Our app is a document-based app that uses UIDocumentBrowserViewController. We are facing an issue when the user is creating a new document on iOS/iPadOS when in the “Recents” tab (as opposed to the “Browse” tab). Specifically, the document is saved to a hidden ubiquity container. As a result, the user cannot find the file in the document browser. It also does not appear in “Recents”. The expected behaviour would be a folder with our app's icon on it on the user’s iCloud Drive, which contains the files the user creates when in the “Recents” tab.
This issue started to happen when we introduced a new feature that uses iCloud Documents with its own ubiquity container. I'm not sure how UIDocumentBrowserViewController handled saving documents to a default location on iCloud Drive before we had the iCloud Documents entitlement enabled. All I know is that there were no issues.
How to recreate the issue:
Create a document-based app with UIDocumentBrowserViewController. Run the app and create a document while in the recents tab with iCloud enabled. The document will be stored to a folder on iCloud.
Now, enable iCloud Documents and specify a ubiquity container. Then, try to create documents in the Recents tab. The location in which the documents are created cannot be navigated to.
I have an app with fairly typical requirements - I need to insert some data (in my case from the network but could be anything) and I want to do it in the background to keep the UI responsive.
I'm using SwiftData.
I've created a ModelActor that does the importing and using the debugger I can confirm that the data is indeed being inserted.
On the UI side, I'm using @Query and a SwiftUI List to display the data but what I am seeing is that @Query is not updating as the data is being inserted. I have to quit and re-launch the app in order for the data to appear, almost like the context running the UI isn't communicating with the context in the ModelActor.
I've included a barebones sample project. To reproduce the issue, tap the 'Background Insert' button. You'll see logs that show items being inserted but the UI is not showing any data.
I've tested on the just released iOS 18b3 seed (22A5307f).
The sample project is here:
https://hanchor.s3.amazonaws.com/misc/SwiftDataBackgroundV2.zip
I am getting the following error while trying to delete a Record Type in Dev container. How do I solve it?
invalid attempt to delete user record type
CKShare provides a url that allows others to be invited. It is necessary for a potential participant to have access to this url (otherwise there is no way for them to accept the invitation). An easy solution is to send this url via the Messages application, but this is an extra step for the share owner. I have noticed that Apple's Passwords app somehow sends this url to the invited user within the Passwords app - and I wonder if this is possible with just public Apple apis, or if Apple uses some private api to achieve this.
I'm using Xcode 16 and SwiftUI targeting iOS 18. I'm new to Core Data, and when I create a new project and select to use Core Data as storage, I get boilerplate code for it.
The problem is that when I try to see the preview without any change to the code, I get a Fatal Error:
CrashReportError: Fatal Error in Persistence.swift
Test crashed due to fatalError in Persistence.swift at line 52.
Unresolved error Error Domain=NSSQLiteErrorDomain Code=8 "(null)" UserInfo={NSFilePath=/Users/monni/Library/Developer/Xcode/UserData/Previews/Simulator Devices/D0D98B5B-7E6F-4DC3-B16A-34D6D2958558/data/Containers/Data/Application/A98879A6-46F5-4E29-B2D7-AD294F1EFFD0/Library/Application Support/Test.sqlite, NSSQLiteErrorDomain=8}, ["NSSQLiteErrorDomain": 8, "NSFilePath": /Users/monni/Library/Developer/Xcode/UserData/Previews/Simulator Devices/D0D98B5B-7E6F-4DC3-B16A-34D6D2958558/data/Containers/Data/Application/A98879A6-46F5-4E29-B2D7-AD294F1EFFD0/Library/Application Support/Test.sqlite]
When I try to open the SQLite database there are no entities in it.
I have also tried xcrun simctl --set previews delete all, but with no luck.
Re SwiftData: is my understanding correct : generally speaking and by default insert method inserts objects into the context and context automatically persist - e.g. inserts them into container while the delete method does not - it only deletes from context and context does not delete them from the container unless save is called ? It is not clear from the documentation nor from the definitions :
public func delete<T>(model: T.Type, where predicate: Predicate<T>? = nil, includeSubclasses: Bool = true) throws where T : PersistentModel
//How can I test it ?
I’m keen to learn where I can confirm this in Apple’s documentation or official articles, code definitions, apart from experimenting or consulting third-party materials. Where does it explicitly state that SwiftData includes an automatic saving feature but does not offer automatic deletion?
"Meet SwiftData" (WWDC23): Around the 14:30 mark, Apple mentions that SwiftData automatically saves changes "at opportune moments." But nothing is advised re deleting ?
Are we supposed to be taking hints :
"Build an app with SwiftData" (WWDC23): This session demonstrates using context.save() to persist changes after deleting an object, implies the idea that deletion isn't automatic
How to truly learn if you do not have official materials ? This is exact Science, not archeology or history.
I feel like a speleologist.
I have two contexts, MAIN and VIEW. I construct an object in MAIN and it appears in VIEW (which I use to display it in the UI). Then I delete the object in MAIN. Because the UI holds a reference to the object in VIEW, VIEW records it as a pending delete (Problem 1). I don't understand why it does this nor can I find this behaviour documented. Docs for deletedObjects say "objects that will be removed from their persistent store during the next save". This has already happened!
(Problem 2) Then I rollback the VIEW context, and the object is resurrected. awakeFromInsert is called again. While the object (correctly) does not appear in a freshly executed fetch request, it does appear in the @FetchRequest of the SwiftUI View which is now displaying stale data. I cannot figure out how to get SwiftUI to execute the fetch request again (I know I can force regeneration of the UI, but would like to avoid this).
This is self-contained demonstration of the problem that can be run in a Playground. Press Create, then Delete (note console output), then Rollback (note console output, and that element count changes from 0 to 1 in the UI)
import CoreData
import SwiftUI
@objc(TestEntity)
class TestEntity : NSManagedObject, Identifiable{
@NSManaged var id : UUID?
override func awakeFromInsert() {
print("Awake from insert")
if id == nil {
// Avoid resetting ID when we resurrect the phantom delete
self.id = UUID()
}
super.awakeFromInsert()
}
class func add(in context: NSManagedObjectContext) -> UUID {
let id = UUID()
context.performAndWait {
let mo = TestEntity(context: context)
mo.id = id
}
return id
}
class func fetch(in context: NSManagedObjectContext) -> [TestEntity] {
let fr = TestEntity.fetchRequest()
return try! context.fetch(fr) as! [TestEntity]
}
}
class CoreDataStack {
// Main is attached to the store
var main : NSManagedObjectContext!
// View is a child context of main and used to display the UI
var view : NSManagedObjectContext!
// Set up a simple entity with an ID attribute
func getEntities() -> [NSEntityDescription] {
let testEntity = NSEntityDescription()
testEntity.managedObjectClassName = "TestEntity"
testEntity.name = "TestEntity"
let idAttribute = NSAttributeDescription()
idAttribute.name = "id"
idAttribute.type = .uuid
testEntity.properties.append(idAttribute)
return [testEntity]
}
init() {
let model = NSManagedObjectModel()
model.entities = getEntities()
let container = NSPersistentContainer(name: "TestModel", managedObjectModel: model)
let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
container.persistentStoreDescriptions = [description]
container.loadPersistentStores { desc, error in
if error != nil {
fatalError("Failed to set up coredata")
}
}
main = container.viewContext
view = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
view.automaticallyMergesChangesFromParent = true
view.parent = main
}
func create() {
let entityId = TestEntity.add(in: main)
main.performAndWait {
try! main.save()
}
}
func delete() {
main.performAndWait {
if let mo = TestEntity.fetch(in: main).first {
main.delete(mo)
try! main.save()
}
}
self.view.perform {
// We only find that we have a pending delete here if we hold a reference to the object, e.g. in the UI via @FetchRequest
if(self.view.deletedObjects.count != 0) {
print("!!! view has a pending delete, even though main has saved the delete !!!")
}
}
}
func rollback() {
self.view.perform {
self.view.rollback()
// PROBLEM We now have a resurrected object. Note that awakeFromInsert
// was called again.
}
}
}
import SwiftUI
import PlaygroundSupport
let stack = CoreDataStack()
struct ContentView: View {
@FetchRequest(sortDescriptors: []) private var entities: FetchedResults<TestEntity>
@State var renderID = UUID()
var body: some View {
VStack {
Text("\(entities.count) elements")
Button("Create") {
stack.create()
}
Button("Delete") {
stack.delete()
}
Button("Rollback") {
stack.rollback()
// PROBLEM After rollback we get the element displaying in
// the UI again, even though it isn't present in a freshly
// executed fetch request.
// The @FetchRequest is picking up the resurrected TestEntity in view
// But not actually issuing a fetch.
self.renderID = UUID()
entities.nsPredicate
}
}.id(renderID)
}
}
//stack.execute()
let view = ContentView()
.environment(\.managedObjectContext, stack.view)
PlaygroundPage.current.setLiveView(view)
After upgrading to iOS 18, my Core Data stack using NSPersistentCloudKitContainer in a shared App Group container stopped syncing correctly. The persistent store configuration, which previously worked in iOS 17, now experiences delayed or missing sync updates between devices. Then the app freezes and writes terminal the same error detail (which I provided) too many times.
The debug logs from the CloudKit mirroring delegate (NSCloudKitMirroringDelegate) show repetitive notifications but no updates in persistent history. Additionally, the persistent history tracking key appears unresponsive to local changes, causing transactions to fail in updating or syncing as expected.
Key setup details:
Core Data is set up within an App Group container using NSPersistentCloudKitContainer.
NSPersistentHistoryTrackingKey and NSPersistentStoreRemoteChangeNotificationPostOptionKey options are set to true.
Any insights into changes in iOS 18 Core Data or CloudKit handling with NSPersistentCloudKitContainer, especially around history tracking and sync delays, would be greatly appreciated. Thank you.
Error Detail
file:///private/var/mobile/Containers/Shared/AppGroup/BF95D309-EBE9-485E-B5CE-AA17097F7B60/[AppName]Database.sqlite
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate managedObjectContextSaved:](3123): <NSCloudKitMirroringDelegate: 0x3032b4870>: Observed context save: <NSPersistentStoreCoordinator: 0x302694bd0> - <NSManagedObjectContext: 0x3036b1a00>
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:](3166): <NSCloudKitMirroringDelegate: 0x3032b4870>: Observed remote store notification: <NSPersistentStoreCoordinator: 0x302694bd0> - 090C4244-0101-4DEF-90D6-1260570F47A5 - <NSPersistentHistoryToken - {
"090C4244-0101-4DEF-90D6-1260570F47A5" = 9;
}> -
Persistence.swift
struct PersistenceController {
let container: NSPersistentCloudKitContainer
static let shared = PersistenceController()
static var preview: PersistenceController = {PersistenceController()}()
init() {
container = NSPersistentCloudKitContainer(name: "[AppName]")
// Configure CloudKit for the default container
if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.[CompanyName].[AppName]") {
let storeURL = url.appendingPathComponent("[AppName]Database.sqlite")
let description = container.persistentStoreDescriptions.first
description?.url = storeURL
description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description?.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.persistentStoreDescriptions = [description].compactMap { $0 }
}
container.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
}
}
So I’m trying to setup an existing app to work with iCloud syncing. The syncing part seems to be working for the most part.
When the app is first installed it sets up some data locally.
Throughout the life of the app additional data is added through updates. There is a user default setup that stores the current data version, then compares it with the new version. If it’s newer, then loads the additional data.
The issue I’ve got is a user can delete the app and reinstall, or install on another device which has that data version as 0, prompting another import even though the data in the cloud is current version, resulting in duplicate data once the sync is done.
How can I persist that version data? I’ve seen NSUbiquitousKeyValueStore which seems to be a cloud based version of user defaults, but it says not to rely on it if it’s critical to app functions