I have SwiftData models containing arrays of Codable structs that worked fine before adding CloudKit capability. I believe they are the reason I started seeing errors after enabling CloudKit.
Example model:
@Model
final class ProtocolMedication {
var times: [SchedulingTime] = [] // SchedulingTime is Codable
// other properties...
}
After enabling CloudKit, I get this error logged to the console:
'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
CloudKit Console shows this times
data as "plain text" instead of "bplist" format.
Other struct/enum properties display correctly (I think) as "bplist" in CloudKit Console.
The local SwiftData storage handled these arrays fine - this issue only appeared with CloudKit integration.
What's the recommended approach for storing arrays of Codable structs in SwiftData models that sync with CloudKit?
Did you wait some time to make sure the error didn't pop up? It might take a minute or two. Try inserting a record then re-running and waiting a few minutes.
Yeah, I've tried waiting until seeing the new record being synchronized across my devices, and haven't seen the issue.
NSKeyedUnarchiveFromData
is the default transformer and comes to play when you use a Core Data / SwiftData transformable
attribute without explicitly specifying a transformer. Unless you are using a transformable
attribute, it shouldn't be engaged.
My best guess is that your CloudKit schema / data contains something that needs a transformer (due to your historical changes?), and that triggers the error when NSPersistentCloudKitContainer
tries to synchronize the data from the server to your device.
If that is the case, consider cleaning up the schema and data on the CloudKit server. Assuming you are on the CloudKit development environment, you can remove the existing schema and data by resetting the environment, and then re-create the schema by adding new records. For more information, see Initialize the CloudKit development schema and Create the CloudKit schema.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.