0

Setup:

My app uses CoreData & CloudKit sync.
Entity Item has among others an attribute status of type Int16 with corresponding property @NSManaged var status: Int16, and an attribute names of type transformable using NSSecureUnarchiveFromDataTransformer with corresponding property @NSManaged var names: Set<String>?

To get notified when iCloud records change, the app uses a query subscription.
When a notification is delivered, an identifier of the changed object is sent, the objectID of the NSManagedObject is obtained, and the corresponding ckRecord: CKRecord is fetched using persistentContainer.record(for: objectID) of persistentContainer: NSPersistentCloudKitContainer to get the actual property values.

Question:

"normal" field values, e.g. status, can easily be obtained using ckRecord["CD_status"] as! Int16. I can also get ckRecord["CD_names"] as! NSData, but I don't know how to convert it back to Set<String>?.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • I guess `NSSecureUnarchiveFromDataTransformer(). transformedValue(ckRecord["CD_names"] as! NSData)` might work? But I think that `NSKeyedUnarchiver.unarchivedObject(ofClasses:from:)` might be enough. – Larme Sep 04 '22 at 11:21
  • Thanks for the suggestion! I used let `names = NSSecureUnarchiveFromDataTransformer(). transformedValue(ckRecord["CD_names"])`and it works (as with `as! NSData`)! Please make your comment to an answer so that I can accept it. – Reinhard Männer Sep 04 '22 at 11:52

1 Answers1

1

Types for properties are limited to some types in CoreData, Set<String> not being one, that's why you need a transformer, that will transform it into (NS)Data.

So, you could use that transformer to transform it back with transformedValue(_:):

NSSecureUnarchiveFromDataTransformer().transformedValue(ckRecord["CD_names"])
Larme
  • 24,190
  • 6
  • 51
  • 81