I have a Multiplatform app for iOS and macOS targets.
I am using CloudKit with CoreData and have successfully established a private and public database.
The app has successfully synced private and public data for months between macOS (dev machine), an iPhone 13 Pro and an iPad Pro 12.9inch 2nd gen. The public data also syncs perfectly to simulator instances running under other iCloud accounts.
Recently I added a new entity in the public DB and here is where I seemed to have made a mistake.
I entered data into the new public database via my developer UI built into the macOS app running on my MBP before I indexed the necessary fields.
Side note - I find it necessary to index the following for each Entity to ensure iCloud sync works as expected on all devices...
- modifiedTimestamp - Queryable
- modifiedTimestamp - Sortable
- recordName - Queryable
Realising my mistake, I indexed the above CKRecord fields for the new Entity.
Since then, the macOS target has remained in some way "frozen" (for want of a better term). I can add new public or private records in the macOS app but they do not propagate to the public or private stores in iCloud.
I have attempted many fixed, some summarised below:
- clean build folder from Xcode;
- remove all files from the folder /Users/<me>/Library/Containers/<my app name>, place in recycle bin, empty recycle bin, then build and run;
- build and run on iPhone and iPad targets to ensure all apps are current dev version, then repeat above processes.
I've read through the console logging when I build and run the macOS app many many times to see whether I can find any hint. The closest thing I can find is...
BOOL _NSPersistentUIDeleteItemAtFileURL(NSURL *const __strong) Failed to stat item: file:///Users/<me>/Library/Containers/com.me.AppName/Data/Library/Saved%20Application%20State/com.me.AppName.savedState/restorecount.plist
but my research on this and other forums suggests this is not relevant.
Through this, the app still functions as expected on iOS devices and both private and public database additions and modifications propagate to iCloud stores and to other devices.
I expect that removing the macOS app entirely from my dev machine would trigger a complete sync with all existing data. Imagine I bought a new macOS device and chose to install my app where before I had run this only on my iOS devices. My current problem suggests that I could not do this, but I know that this is not the intended behaviour.
This scenario makes me think there is a setting file for my macOS app that I'm not aware of and that this impeding the sync of all existing app data back to the fresh install of the macOS app? But that is a wild guess.
Running public releases (no betas)
- Xcode 15.4 (15F31d)
- macOS Sonoma 14.5
- physical iOS devices running iOS 17.5.1
Any words of wisdom on how I might go about trying to solve this problem please?
Problem solved.
When compared to another project I’m working on, I noticed something missing from the “Signing & Capabilities” under macOS App Sandbox…
Once I checked Outgoing Connections (Client), Xcode created a new entitlements file (oddly named "AppNameDebug.entitlements") and added the setting "Outgoing Network Connections = YES".
Following my next build and run, sync to CloudKit databases worked perfectly.
Can’t figure why this wasn’t required before. I didn't uncheck this and the previous entitlements file did not contain this line item before.
All I can suggest is that I added an #if DEBUG
macro to my project around the same time, so maybe this triggered the requirement?
#if DEBUG
do {
// Use the container to initialize the development schema.
try container.initializeCloudKitSchema(options: [])
} catch {
// Handle any errors.
}
#endif
But main problem solved.
If anyone has an explanation that would be appreciated.