Understanding CloudKit Communication

I have been playing around with CloudKit and I was under the impression that the user needed to be logged-in into iCloud in order to use CloudKit, but I don't think it's true.

I'm testing an app that successfully communicates with the private database in CloudKit, I can read and write records to it. To my surprise I logged-off from my iCloud account and from the Sandbox Account and I can still read and write records from CloudKit which was unexpected, I was thinking that after logging off I wouldn't be able to communicate with CloudKit, in fact, I deleted the app from my phone, recompiled in Xcode and the app can still communicate with CloudKit.

Is this normal behavior, does CloudKit works without the user being logged into iCloud?

Thanks



Answered by fsdolphin in 613940022
Here is the code I'm using...

Connection to the Private Database
Code Block
let privateDatabase = CKContainer.default().database(with: .private)


Reading records
Code Block
`privateDatabase.perform(query, inZoneWith:nil){( results , error) in ... }


Writing Records
Code Block
privateDatabase.save(ckRecord){ (record, error) in ...}


What I ultimately need to know is whether I need to check if the user is logged in or not when accessing the database to avoid redundant code/checks.

Thanks

Hello there,

An iCloud account is required to read or write from the private and shared databases, or to write in the public database.

Enqueueing database operations which attempt to perform any of the following actions should produce an error.

See:
https://developer.apple.com/documentation/cloudkit/ckerrorcode/ckerrornotauthenticated

Can you share specific code which succeeds without a signed-in iCloud account on the device?


Accepted Answer
Here is the code I'm using...

Connection to the Private Database
Code Block
let privateDatabase = CKContainer.default().database(with: .private)


Reading records
Code Block
`privateDatabase.perform(query, inZoneWith:nil){( results , error) in ... }


Writing Records
Code Block
privateDatabase.save(ckRecord){ (record, error) in ...}


What I ultimately need to know is whether I need to check if the user is logged in or not when accessing the database to avoid redundant code/checks.

Thanks

PrivateDatabase operations should be expected to fail if the user is not signed into an iCloud account on the device running the code. As mentioned earlier, CKErrorNotAuthenticated is the error you should expect to see. If you are seeing these operations succeed on a device in which no iCloud account is signed in, please file a report via Feedback Assistant for further investigation.

You don't need to explicitly check for account status prior to each operation, but you should handle CKErrorNotAuthenticated.

Additionally, see this video from WWDC16 (starting at 41:08) for advice on best practices around registering for CKAccountChanged notifications to better handle these kinds of cases.
Thank you for the clarification.

FYI - I accidentally marked my previous response as the correct answer and I don't see a way of changing it, I need to mark your last comment as the correct answer.
Understanding CloudKit Communication
 
 
Q