问题
Im trying building my application from Xcode11 beta on phone IOS 13 Beta. I have crash when application loading.
2019-07-22 13:58:12.910460+0300 GoodWine[3738:792501] [error] fault: One or more models in this application are using transformable properties with transformer names that are either unset, or set to NSKeyedUnarchiveFromDataTransformerName. Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead. At some point, Core Data will default to using "NSSecureUnarchiveFromData" when nil is specified, and transformable properties containing classes that do not support NSSecureCoding will become unreadable.
CoreData: fault: One or more models in this application are using transformable properties with transformer names that are either unset, or set to NSKeyedUnarchiveFromDataTransformerName. Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead. At some point, Core Data will default to using "NSSecureUnarchiveFromData" when nil is specified, and transformable properties containing classes that do not support NSSecureCoding will become unreadable.
2019-07-22 13:58:12.910595+0300 GoodWine[3738:792501] [error] CoreData: One or more models in this application are using transformable properties with transformer names that are either unset, or set to NSKeyedUnarchiveFromDataTransformerName. Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead. At some point, Core Data will default to using "NSSecureUnarchiveFromData" when nil is specified, and transformable properties containing classes that do not support NSSecureCoding will become unreadable.
CoreData: warning: Property 'value' on Entity 'SFMCKeyValueEntity' is using nil or an insecure NSValueTransformer. Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead.
回答1:
I found this solution. The errors have disappeared and it would seem to work. I keep testing.
For all Transformable attributes, I have set “Transformer” to “NSSecureUnarchiveFromData” in the Data Model Inspector panel. (Image for clarification)
EDIT:
After a few days of testing I add something to my previous solution.
The previous solution works if, after the changes, the application is deleted. Otherwise, the data model is not recognized and is created from scratch, losing all the historical information (and in production this is not acceptable!!!).
The final solution I adopted was to add these changes to a new model (following this link) and implementing the migration (always described in the link).
In this case the warning appears only once after the update and then disappears.
回答2:
My solution was to stay with old default transformers in order to avoid any compatibility issues. The issues may arise since default (nil) transformers will be replaced with NSSecureUnarchiveFromData eventually, I'm not sure if previously persisted values would be decoded properly (they were encoded by default transformer but after the update they will be decoded by NSSecureUnarchiveFromDataTransformer). I've implemented an explicit default transformer for fields of NSDictionary, NSArray, NSSet foundation types that comply to NSCoding out of the box:
@objc(DefaultTransformer)
class DefaultTransformer: ValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSData.self
}
override open func reverseTransformedValue(_ value: Any?) -> Any? {
guard let value = value as? Data else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: value)
}
override class func allowsReverseTransformation() -> Bool {
return true
}
override func transformedValue(_ value: Any?) -> Any? {
guard let value = value else {
return nil
}
return NSKeyedArchiver.archivedData(withRootObject: value)
}
}
For my special datatypes that comply NSCoding I've implemented particular transformers as follows:
@objc(EmailTransformer)
class EmailTransformer: ValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSData.self
}
override open func reverseTransformedValue(_ value: Any?) -> Any? {
guard let value = value as? Data else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: value)
}
override class func allowsReverseTransformation() -> Bool {
return true
}
override func transformedValue(_ value: Any?) -> Any? {
guard let value = value as? [Email] else {
return nil
}
return NSKeyedArchiver.archivedData(withRootObject: value)
}
}
After that, I set these transformers for the transformable fields, thus explicitly opted the safest way. Pros of the solution: you don't need to bother with data migration, just implement explicit transformers and set them to any data model version you already have.
来源:https://stackoverflow.com/questions/57144796/coredata-crash-error-xcode-11-beta-ios-13-beta