'Cloudkit Public Database Error: "WRITE operation not permitted"

I have currently a problem with the CloudKit Public Database.

Sometimes I receive an error when modifying records: Permission Failure" (10/2007); server message = "WRITE operation not permitted"; The Error Code is 10 permissionFailure

If I have a low number of record modifies I almost never get the error but as soon as I have more modifies (3000+ per hour) errors become overproportional frequent and more than 20 percent of all request result in this error. This leads me to the fact, that my code must be okay, since the error only happens with increasing CloudKit activity.

Of course I (and all the users of the app) are signed in to iCloud and have iCloud Drive activated. The WRITE permission is activated for everyone, too.

Also the request rate is far below the limit of 40 requests per second (maximum is around 3 requests per second when many users are active).

Does anyone has an idea?



Solution 1:[1]

Same problem; no idea what the cause is, but happened in a test scenario switching from one iCloud account to another. Perhaps some CloudKit state got latched onto a token from the previous account.

Solved by signing out of iCloud (again) and signing in again. I'm going to implement feedback to the user of the kind "if this problem persists, sign out... sign in...". I'm hoping it will be very rare.

Solution 2:[2]

The reason for my instance of this problem: switching from one iCloud account to another was not properly purging the local cache (including metadata in Core Data), resulting in the new account occasionally attempting an update of cloud records belonging to the previous user.

The fix for this type of situation depends on the application. In my case a clean slate switch is required (purge all data after confirmation to switch account). Other applications may need cloning of the data into newly created cloud records (i.e. with correct creator/owner metadata).

Solution 3:[3]

When different iCloud users try to update the same CKRecord object in a public CloudKit database with default security role settings, then this may result in a "WRITE operation not permitted" error.

To fix this, extend security role _icloud with Write permission for the record types you want to be updatable by multiple iCloud users.

Security roles can be edited in the Development environment of a database in CloudKit Dashboard. After editing, you must commit the changes to Production.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Myers
Solution 2 Pierre van Aswegen
Solution 3 Ely