I have just built my first app that uses CloudKit and is working fine in the development environment. I have now deployed my container to Production but while testing in this production environment, the app does not work.
The user creates a profile on the app. This is a CKRecord created from Container.default().userRecordID() and is then subsequently stored on a public container in CloudKit. This works perfectly in the development environment.
For beta testing using TestFlight, I deployed my container to Production. The userRecordID in both development and production is the same. However, the CKRecord I fetch using this userRecordID returns a nil value and the reason my app is not working. I'm able to fetch a CKRecord in the development environment.
Would be very grateful if anyone can help me with I'm missing.
I have tested this app using a simulator. If the record itself is returning a nil value, I doubt my app will work on the device. I have created the com.apple.developer.icloud-container-environment in the Entitlements file and set it to Production.
Below is the code snippet Xcode - 14.0.1 iOS - 16.0
import CloudKit
final class CloudKitManager {
static let shared = CloudKitManager()
private init(){}
var userRecord: CKRecord?
var profileRecordID: CKRecord.ID?
var clinicRecordID: CKRecord.ID?
let container = CKContainer.default()
var getUserRecordHasFired : Bool = false
var accountStatus: CKAccountStatus = .couldNotDetermine
func checkAccountStatus() async throws -> CKAccountStatus {try await container.accountStatus()}
func getAccountStatus() async {
do{
accountStatus = try await checkAccountStatus()
print(accountStatus)
} catch{
print(error.localizedDescription)
}
}
func getUserRecord() async throws {
let recordID = try await container.userRecordID()
print(recordID) //This returns a recordID in both development and production.
let record = try await container.publicCloudDatabase.record(for: recordID)
userRecord = record
print(record["userProfile"]) //This returns a CKRecord in development but nil in production.
if let profileReference = record["userProfile"] as? CKRecord.Reference {
profileRecordID = profileReference.recordID
print("profile record ID \(profileRecordID)")
guard let profileRecordID = profileRecordID else {return}
let profileRecord = try await container.publicCloudDatabase.record(for: profileRecordID)
let profile = DCProfile(record: profileRecord)
clinicRecordID = profile.hasClinic.recordID
}
getUserRecordHasFired = true
}
}