While implementing Apple’s CloudKit Catalog, I discovered an error in the sample code for the saveRecords method under the Records item.
The example demoSaveRecords method takes a lot of possible arguments.
|
|
Using these parameters, the demoSaveRecords method implementation constructs a record object and an options argument that are passed to the saveRecords method on CloudKit.Database.
Within the method’s code is this offending block…
The is intended to create a property on the record object using the keys and values of the fields object passed in as a parameter. Presumably, the fields object you pass into the method will look like this example provided in the the CloudKit JS Reference documentation:
Likewise, if you were to simply copy the fields property off of an existing record and provide it as the method parameter, this example is exactly how the fields property is constructed.
The problem is that the code block in Apple’s demoSaveRecords function adds an extra nested value: property to the mix. When the CloudKit.Database saveRecords method is expecting:
|
|
Instead it gets:
|
|
The evidence is a 400 Bad Request error that likely reads:
missing required field ‘recordName’
or
Invalid value, expected type STRING but actual value was object of class com.apple.cloudkit.ws.application.common.data.UnknownTypeModel
The fix is to simply to replace…obj[key] = { value: fields[key] };
withobj[key] = fields[key];