Search code examples
ioscore-dataicloudcloudkit

Getting "Invalid bundle ID for container" error when using NSPersistentCloudKitContainer


I have a workable CoreData app (local storage).

I would like to make it workable with iCloudKit.

After migrating from NSPersistentContainer to NSPersistentCloudKitContainer (CoreDataStack.swift, line 21), I was hoping I can achieve success in the following testing.

  1. Launch the app
  2. Write a CoreData record for the first time.
  3. Hopefully the data will then quietly sync to iCloud from device.
  4. Delete the app
  5. Install and launch the app again
  6. Hopefully the old data before uninstall will quietly sync from iCloud to device.
  7. Able read the old data before uninstall

However, I get stuck in step 2. After several attempts, I am still getting the following iCloud related error.


Here's the complete error log.

CoreData: debug: CoreData+CloudKit: -[PFCloudKitOptionsValidator validateOptions:andStoreOptions:error:](36): Validating options: <NSCloudKitMirroringDelegateOptions: 0x600001c0d050> containerIdentifier:iCloud.com.yocto.xxx databaseScope:Private ckAssetThresholdBytes:<null> operationMemoryThresholdBytes:<null> useEncryptedStorage:NO useDeviceToDeviceEncryption:NO automaticallyDownloadFileBackedFutures:NO automaticallyScheduleImportAndExportOperations:YES skipCloudKitSetup:NO preserveLegacyRecordMetadataBehavior:NO useDaemon:YES apsConnectionMachServiceName:<null> containerProvider:<PFCloudKitContainerProvider: 0x600002c08770> storeMonitorProvider:<PFCloudKitStoreMonitorProvider: 0x600002c08730> metricsClient:<PFCloudKitMetricsClient: 0x600002c087d0> metadataPurger:<PFCloudKitMetadataPurger: 0x600002c08790> scheduler:<null> notificationListener:<null> containerOptions:<null> defaultOperationConfiguration:<null> progressProvider:<NSPersistentCloudKitContainer: 0x600003b00b00> test_useLegacySavePolicy:YES archivingUtilities:<PFCloudKitArchivingUtilities: 0x600002c08800>
storeOptions: {
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSPersistentCloudKitContainerOptionsKey = "<NSPersistentCloudKitContainerOptions: 0x60000204f0f0>";
    NSPersistentHistoryTrackingKey = 1;
    NSPersistentStoreMirroringOptionsKey =     {
        NSPersistentStoreMirroringDelegateOptionKey = "<NSCloudKitMirroringDelegate: 0x600001504a90>";
    };
}
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate observeChangesForStore:inPersistentStoreCoordinator:](385): <NSCloudKitMirroringDelegate: 0x600001504a90>: Observing store: <NSSQLCore: 0x131e151a0> (URL: file:///Users/yccheok/Library/Developer/CoreSimulator/Devices/F1F59F93-1800-4D43-AAE7-FD942D440B10/data/Containers/Data/Application/54000F75-F12A-4FF2-A2A1-545ACE41A367/Library/Application%20Support/xxx.sqlite)
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _setUpCloudKitIntegration](530): <NSCloudKitMirroringDelegate: 0x600001504a90>: Successfully enqueued setup request.
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest](2928): <NSCloudKitMirroringDelegate: 0x600001504a90>: Checking for pending requests.
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest]_block_invoke(2941): <NSCloudKitMirroringDelegate: 0x600001504a90>: Executing: <NSCloudKitMirroringDelegateSetupRequest: 0x600003b51180> 428D83F5-B1E4-4BC6-B411-A9D0583983F0
success write Optional("title 0"), Optional("body 0")
2022-04-10 21:40:58.519262+0800 xxx[2918:70575] [error] error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1106): <NSCloudKitMirroringDelegate: 0x600001504a90>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x131e151a0> (URL: file:///Users/yccheok/Library/Developer/CoreSimulator/Devices/F1F59F93-1800-4D43-AAE7-FD942D440B10/data/Containers/Data/Application/54000F75-F12A-4FF2-A2A1-545ACE41A367/Library/Application%20Support/xxx.sqlite)
<CKError 0x6000020516e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx"; partial errors: {
    com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2>
}>
CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1106): <NSCloudKitMirroringDelegate: 0x600001504a90>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x131e151a0> (URL: file:///Users/yccheok/Library/Developer/CoreSimulator/Devices/F1F59F93-1800-4D43-AAE7-FD942D440B10/data/Containers/Data/Application/54000F75-F12A-4FF2-A2A1-545ACE41A367/Library/Application%20Support/xxx.sqlite)
<CKError 0x6000020516e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx"; partial errors: {
    com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2>
}>
2022-04-10 21:40:58.519485+0800 xxx[2918:70575] [error] error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2115): <NSCloudKitMirroringDelegate: 0x600001504a90> - Attempting recovery from error: <CKError 0x6000020516e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx"; partial errors: {
    com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2>
}>
CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2115): <NSCloudKitMirroringDelegate: 0x600001504a90> - Attempting recovery from error: <CKError 0x6000020516e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx"; partial errors: {
    com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2>
}>
2022-04-10 21:40:58.528209+0800 xxx[2918:70575] [error] error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:]_block_invoke(2464): <NSCloudKitMirroringDelegate: 0x600001504a90>: Found unknown error as part of a partial failure: <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx">
CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:]_block_invoke(2464): <NSCloudKitMirroringDelegate: 0x600001504a90>: Found unknown error as part of a partial failure: <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx">
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _finishedRequest:withResult:](2959): Finished request: <NSCloudKitMirroringDelegateSetupRequest: 0x600003b51180> 428D83F5-B1E4-4BC6-B411-A9D0583983F0 with result: <NSCloudKitMirroringResult: 0x600002e6db00> success: 0 madeChanges: 0 error: <CKError 0x6000020516e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2; container ID = "iCloud.com.yocto.xxx"; partial errors: {
    com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002051b60: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 94EEBD82778D22EC; uuid = 340ED591-A422-4E70-8BC1-EC300A8C34C2>
}>
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest](2928): <NSCloudKitMirroringDelegate: 0x600001504a90>: Checking for pending requests.
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest]_block_invoke(2944): <NSCloudKitMirroringDelegate: 0x600001504a90>: No more requests to execute.

The following error draws my attention

Invalid bundle ID for container


I step-by-step the check, to ensure I have done the thing correctly.

Xcode Target Settings

Enter image description here

  1. My app Bundle identifier is com.yocto.xxx
  2. My iCloud container is iCloud.com.yocto.xxx

iCloudKit Console

Enter image description here

  1. I can see the container iCloud.com.yocto.xxx is created in iCloudKit console.
  2. I am still do not see my own app schema yet. It is a simple entity called Todo with two columns - https://github.com/yccheok/xxx/blob/main/xxx/xxx.xcdatamodeld/xxx.xcdatamodel/contents

Sandbox tester account

Enter image description here

  1. I created a sandbox tester account from App Store Connect/ Users and Access.
  2. I then use the sandbox tester account, to login from web to https://www.icloud.com/. Login doesn't have any issues.

Login to Simulator

Enter image description here

  1. Login to simulator doesn't have any issue

Minimal reproducible example

Here's the Xcode I use for this testing

https://github.com/yccheok/xxx


What went wrong in between, which causes me to keep getting Invalid bundle ID for container? What other steps could I try?


Solution

  • This is how I solve the issue.

    I was referring to this answer - CloudKit - "Invalid bundle ID for container"

    What I did wrong was

    1. I deleted my app id from https://developer.apple.com/account/resources/identifiers/list

    2. I rerun the app via Xcode, and expecting the app id will be created automatically. But it doesn't. The app id will not be automatically created even if I run the app from Xcode.

      Enter image description here


    So, what I did right was:

    1. I create a new project with a new app id, and using a new iCloud container id too.

    2. I launch the app for the first time.

    3. From https://developer.apple.com/account/resources/identifiers/list, I click on the app id as shown in the following screenshot.

      Enter image description here

      I untick and tick the checkbox for iCloud, click Save button in the top right.

    Now, I can observe the data is being saved to iCloud. I have verified by uninstalling the app, installing the app and performing read. There isn't any issue of reading the old data.