How TO USE CLOUDKIT Development OR Production DB in Xcode debugging (using Xcode v 8.2)
Not to worry, if you adhere to the following directions, it will insure you will be able to configure Xcode to work with CloudKit and use BOTH the CloudKit Development AND PRODUCTION DB IN XCODE and to deploy your project to the App Store
1) Create an iCloud account on the xcode computer and login to it
2) Configure Xcode to use CloudKit from the the Capabilities tab of the Project “menu” by turning on iCloud and selecting the CloudKit box. Xcode should then automatically generate Provisioning Profiles, etc as is shown by the various boxes being checked in the iCloud tab area
3) Click on the “CloudKit Dashboard” box in order to bring up CloudKit. I don’t know why, but there are times you cannot connect to CloudKit, the sign on screen just keeps asking for your userId and password and gives no error message…..just keep trying and you will eventually be able to connect
4) Develop your Records and Fields Schema in the Development database of CloudKit
5) Use CloudKit in your project by following the many blogs on the net to CRUD data in your project (especially StackOverflow.com)
6) When your project is done being debugged and is working using Cloudkit’s Development database, switch to the Production database in order to debug it, as when you deploy the project to the App Store, the App Store (and TestFlight) only use the Production database.
As usual, the Apple documentation about how and when to use the Production database is often confusing, overly complex, contradictory, out of date, or just downright incomprehensible (i.e. written by programmers). Also, many of the blogs concerning this issue are not up to date with the latest Xcode and iOS releases or just plain wrong.
The following is what I found to work, mostly by trial and error (and thankfully finding some blogs that were correct) by saving (duplicating) the project at various points using Finder when I got a successful build.
When something didn’t work (e.g. Deploying the CloudKit Development DB to the Production DB in the CloudKit IDE) I would then bring back the last project copy that worked and try something different.
By the way, I have learned the hard way to follow this procedure (i.e., duplicate and save project via Finder) before making ANY significant changes to Xcode, iOS, CloudKit, my Xcode project settings (plist, entitlements file, build settings, etc, etc), or significant code changes.
To change to Production database I did the following, duplicating and saving the project after each step along the way…
After successful debugging your project using the CloudKit Development DB (which Xcode configures automatically for you when you turn on iCloud and select CloudKit from the Capabilities tab of the Project settings “menu”). I did the following…..(also make sure you are logged into iCloud on your development computer)
1) I added this to my entitlements file (a file located in the left hand “Project” menu of the Xcode IDE).
com.apple.developer.icloud-container-environment and set it to "Development"
Ran on iPhone and it compiled and worked..made a project copy
2) In the CloudKit IDE, I Deployed (select “Deployment” in lower left of CloudKit IDE) the the Development Db SCHEMA to Production (upper right of IDE). This copies ONLY the DB schema but not the actual data. As far as I can tell there is no way to copy the data over from the Development to the Production DB using either CloudKit or Xcode.
3) Then I selected “Production” in the lower left corner of the CloudKit IDE. The CloudKit IDE background color then changes to indicate you are going to be connected to the Production DB. There is NO need to change any code in your project in order to connect to the Production DB.
4) The project then still compiled successfully. BUT, when I ran the project, it STILL connected to the DEVELOPMENT Db
Duplicate, label and save the project using Finder
5) Then I changed “Development” to “Production” as the value of
com.apple.developer.icloud-container-environment in the entitlements file of step 1. All the “boxes” still remained checked successfully in the iClould area of the “Capabilities” tab of the Project settings “menu”
The project then still compiled successfully. This time when I ran the project IT CONNECTED TO THE PRODUCTION DB!!!
This shows that it IS possible to debug your project in Xcode USING THE PRODUCTION DB by following steps 1-5 above
Duplicate, label and save the project using Finder
6) Go to iTunes Connect and create and configure your project in order to get ready to upload your project to the App Store (make sure all required project icons and screen shots have 1st been created in Xcode of course..(by the way…..the Production DB will only work on an actual Device….it will NOT work in the Simulator….to change back and forth between using the Development DB and the Production DB just change the value of step 1 to Production or Development)
6) Select “Generic iOS Device”, then select “Archive” from the Product menu, then select “Upload to App Store”