Hello everyone,
I'm currently working on an iOS app using SwiftUI and Core Data, integrating CloudKit for data synchronization. I've set up my Core Data stack with NSPersistentCloudKitContainer, and everything appears to be working correctly locally. However, I'm not seeing any records appearing in the CloudKit Dashboard. This issue started occurring after transferring the Apple Developer account ownership and changing the CloudKit container.
Here's a summary of my setup and what I've tried so far:
Setup
PersistenceController.swift
import SwiftUI
import Foundation
import CoreData
import CloudKit
class PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "Model")
guard let description = container.persistentStoreDescriptions.first else {
fatalError("No Descriptions found")
}
description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.company.Project")
container.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
container.viewContext.automaticallyMergesChangesFromParent = true
}
func saveContext() {
let context = container.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
static let preview: PersistenceController = {
let controller = PersistenceController()
// Remove existing preview data
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = Letter.fetchRequest()
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
let userFetchRequest: NSFetchRequest<NSFetchRequestResult> = User.fetchRequest()
let userBatchDeleteRequest = NSBatchDeleteRequest(fetchRequest: userFetchRequest)
let senderFetchRequest: NSFetchRequest<NSFetchRequestResult> = Sender.fetchRequest()
let senderBatchDeleteRequest = NSBatchDeleteRequest(fetchRequest: senderFetchRequest)
do {
try controller.container.viewContext.execute(batchDeleteRequest)
try controller.container.viewContext.execute(userBatchDeleteRequest)
try controller.container.viewContext.execute(senderBatchDeleteRequest)
try controller.container.viewContext.save()
} catch {
fatalError("Failed to delete preview data: \(error)")
}
}
Entitlements.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.company.Project</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
</dict>
</plist>
What I've Tried
Verified that the iCloud capability is enabled in the Xcode project settings.
Checked the containerIdentifier in the NSPersistentCloudKitContainer setup.
Ensured the app is signed in with the correct iCloud account.
Observed that local Core Data operations work correctly and save without errors.
Waited to ensure any potential synchronization delays are accounted for.
Observations
I see debug messages in the Xcode console indicating that records are being serialized and saved to CloudKit, but they do not appear in the CloudKit Dashboard.
I have verified that I'm looking at the correct Private Database and _defaultZone.
When I delete the app and reinstall it, I can see that the data remains, indicating that the data is being stored somewhere in iCloud but not visible in the CloudKit Dashboard.
After resetting the schema in the CloudKit Console and running the app, the schema (including record types) syncs immediately, but the records are still not visible.
Console Output
CoreData: debug: CoreData+CloudKit: -[PFCloudKitSerializer newCKRecordsFromObject:fullyMaterializeRecords:includeRelationships:error:](576): Serializer has finished creating record: <CKRecord: 0x15b13e600; recordType=CD_Letter, recordID=F879D7B8-0338-418D-A330-6B9DF7947C6A:(com.apple.coredata.cloudkit.zone:__defaultOwner__), values={
"CD_content" = "Dear User. Happy Valentine's Day! Today. l want to remind you of how much you mean to me. Your presence in my life fills my heart with joy and love. Thank you for being my confidant, my friend, and my love. Every moment with you is precious, and I look forward to creating many more beautiful memories together. With all my love, Thomas";
"CD_createdAt" = "2024-07-30 03:17:13 +0000";
"CD_date" = "2024-07-30 03:15:58 +0000";
"CD_emotion" = Lovely;
"CD_entityName" = Letter;
"CD_icon" = "❤️🔥";
"CD_id" = "81569A99-E74C-43AF-B346-220A75EA336E";
"CD_imageData" = "{ length=282816, sha256=a76343766534e061472e243deec7f0b428c85e8a6b94e6cd761443c45f5be41c }";
"CD_primaryAlpha" = "0.4313725490196079";
"CD_primaryBlue" = 0;
"CD_primaryGreen" = "0.09014883061658401";
"CD_primaryRed" = "0.4156862745098039";
"CD_secondaryAlpha" = 1;
"CD_secondaryBlue" = "0.8823529411764706";
"CD_secondaryGreen" = "0.8784313725490196";
"CD_secondaryRed" = "0.9490196078431372";
"CD_sender" = "EF893D7E-E9E9-453B-B76E-6A5D77E14AA3";
"CD_tertiaryAlpha" = 1;
"CD_tertiaryBlue" = "0.8823529411764706";
"CD_tertiaryGreen" = "0.8784313725490196";
"CD_tertiaryRed" = "0.9490196078431372";
"CD_title" = "I love you";
}>
Despite this, no records are found in the CloudKit Dashboard.
Request for Assistance
Are there any additional steps I might have missed to ensure that records sync correctly with CloudKit?
Could there be any known issues or additional configurations required for syncing Core Data with CloudKit?
Any advice on further troubleshooting steps or areas to investigate would be greatly appreciated.
Thank you for your time and assistance!