Search code examples
iosswiftcore-datacloudkitnspersistentcloudkitcontainer

How to read arrays with NSPersistentCloudKitContainer (CloudKit with Core Data)?


I have [String] and [Int] attributes in my entity on Core Data/type on CloudKit, where they are String list and Int64 list respectively. I'm trying to synchronize them via NSPersistentCloudKitContainer (public server). All other attributes synchronize properly, but these two do not at all. The other ones are simple - String, Date, Int64/Int. I want to make them sync, too.

Both of them are Transformable, with their respective Custom class set to [String] and [Int]. The codegen is Manual/None, so I can create unwrapped attributes for a later use.

When I run the app at first launch I get the CKRecord with their data, too - I mean I see in the debug their values in the downloaded records information. However, when I check Core Data, it tells me nil.

I've tried setting both NSSecureUnarchiveFromData and NSSecureUnarchiveFromDataTransformerName, it didn't work out.

It's clear to me that the arrays' data is downloaded, I don't understand why Core Data doesn't like the arrays.


Solution

  • Okay, managed to solve it:

    1. It must not be a List of any sort on CloudKit, but Bytes.

    2. The rest is simply converting the arrays into NSData when uploading. I did it with Data

      let archivedString = try? NSKeyedArchiver.archivedData(withRootObject: [String], requiringSecureCoding: true)

    Same goes for [Int].