NSAttributedString in Core Data (iOS 14)

I had an app built using SwiftUI and used to work without any issues, but when I build it for iOS 14 the app became very slow and the console began showing this message:

'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release

After investigating, I concluded that using NSAttributedString in Core Data as Transformable object is the reason.

Does anyone has a better way for storing NSAttributedString in Core Data for iOS 14 ?
How have you defined the Transformer?
@OOPer I wanted to upload a picture for the attribute information, but I didn't find a way to do so in the forum.
Anyway, you can find its information below:
  • Attribute Name: content

  • "Type": Transformable

  • NON-OPTIONAL

  • NON-TRANSIENT

  • NON-DERIVED

  • "Transformer": EMPTY

  • "Custom Class": NSAttributedString

  • "Module": EMPTY




Thanks for showing the definition.

Have you tried setting NSSecureUnarchiveFromDataTransformer to "Transformer"?
@OOPer I have just tried what you said. The app crashed with the following error:

>>> Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object of class NSConcreteMutableAttributedString is not among allowed top level class list (
NSArray,
NSDictionary,
NSSet,
NSString,
NSNumber,
NSDate,
NSData,
NSURL,
NSUUID,
NSNull
)'

Thanks for testing. Seems you may need to define your own custom Transformer for NSAttributedString (without using NSKeyedUnarchiveFromData or similar) by yourself.
As far as I tried in a simple sample project, this Transformer worked.
Code Block
import UIKit
import CoreData
@objc(NSAttributedStringTransformer)
class NSAttributedStringTransformer: NSSecureUnarchiveFromDataTransformer {
override class var allowedTopLevelClasses: [AnyClass] {
return super.allowedTopLevelClasses + [NSAttributedString.self]
}
}


I have ended up using a separate entity for each attribute (color, alignment, etc) and the application is working now as expected.

Thank you @OOPer for your help.

NSAttributedString in Core Data (iOS 14)
 
 
Q