Crash when adopting NSSecureUnarchiveFromDataTransformer for a Transformable property

喜你入骨 提交于 2021-02-07 06:47:15

问题


In iOS 12 Apple introduced NSSecureUnarchiveFromDataTransformerName for use on CoreData model entities' Transformable properties. I used to keep the Transformer Name field empty, which implicitly used NSKeyedUnarchiveFromDataTransformerName. This transformer is now deprecated, and keeping the field empty in the future will mean NSSecureUnarchiveFromDataTransformerName instead.

In iOS 13, if that field is empty, you now get a runtime warning telling you the aforementioned. I couldn't find any documentation on this anywhere, the only reference I got was a WWDC 2018 Core Data Best Practices talk which briefly mentioned what I just said.

Now I have a model with an entity which directly stores HTTPURLResponse objects in a Transformable property. It conforms to NSSecureCoding, and I checked in runtime that supportsSecureCoding is true.

Setting NSSecureUnarchiveFromDataTransformerName for the Transformer Name crashes with this message:

Object of class NSHTTPURLResponse is not among allowed top level class list (
    NSArray,
    NSDictionary,
    NSSet,
    NSString,
    NSNumber,
    NSDate,
    NSData,
    NSURL,
    NSUUID,
    NSNull
) with userInfo of (null)

So it sounds like Transformable properties can only be of these top level objects.

I tried subclassing the secure transformer and override the allowedTopLevelClasses property as suggested by the documentation:

@available(iOS 12.0, *)
public class NSSecureUnarchiveHTTPURLResponseFromDataTransformer: NSSecureUnarchiveFromDataTransformer {

    override public class var allowedTopLevelClasses: [AnyClass] {
        return [HTTPURLResponse.self]
    }
}

Then I'd imagine I can create a custom transformer name, set it in the model and call setValueTransformer(_:forName:) for that name, but I couldn't find API to set the default NSKeyedUnarchiveFromDataTransformer for my custom name in case I'm on iOS 11.

Keep in mind, I'm using Xcode 11 Beta 5, but this doesn't seem to be related if I am to accept the meaning of the error I'm getting as stated.

Appreciate any thoughts.


回答1:


I tried to use NSSecureUnarchiveFromDataTransformer also (although I don't need secure coding, see below), but I did not have success. Thus I used a custom value transformer instead. My steps were:

I implemented my custom value transformer class:

@objc(MyTransformer)
class MyTransformer: ValueTransformer {

    override class func setValueTransformer(_ transformer: ValueTransformer?, forName name: NSValueTransformerName) {
        ValueTransformer.setValueTransformer(transformer, forName: name)
    }

    override func transformedValue(_ value: Any?) -> Any? {
        guard let value = value else { return nil }
        let data = serialize(value) // A custom function, e.g. using an NSKeyedArchiver
        return data as NSData
    }

    override class func allowsReverseTransformation() -> Bool {
        return true
    }

    override func reverseTransformedValue(_ value: Any?) -> Any? {
        guard let value = value else { return nil }
        guard let data = value as? Data else { return nil }
        let set = deserialize(data) // A custom function, e.g. using an NSKeyedUnarchiver
        return set as NSSet // Or as an NSArray, or whatever the app expects
    }
}

extension NSValueTransformerName {
    static let myTransformerName = NSValueTransformerName(rawValue: „MyTransformer")
}  

The 1st line (@objc) is required, see this post! Otherwise coreData does not recognise the custom transformer!

Next, I implemented a computed property in the app delegate, according to this post:

private let transformer: Void = {
    MyTransformer.setValueTransformer(MyTransformer(), forName: .myTransformerName)
}()  

It is important to do this early, e.g. in the app delegate, so that coreData does recognise the transformer when it is initialised.

Eventually, I set in the attribute inspector of the transformable attribute in the xcdatamodeld file the Transformer value to MyTransformer.

Then the code run correctly without run time logs.
Please note: In my case, it was not necessary to do secure coding, but the code above can easily be modified to use secure coding instead. Just modify the functions serialize and deserialize accordingly.

EDIT (due to the comment of kas-kad below):

Sorry, my code was unfortunately not complete.

In the app delegate, I used the following computed property (see this link). This ensures that the value transformer is registered very early, even before init is run.

private let transformer : Void = {
    let myTransformer = MyValueTransformer()
    ValueTransformer.setValueTransformer(myTransformer, forName:NSValueTransformerName("MyValueTransformer"))
}()  

And to override class func setValueTransformer does in my implementation obviously nothing. I copied it from somewhere (cannot remember). So one can surely omit it.

The extension of NSValueTransformerName does nothing more than to allow to use .myTransformerName as the transformer name.




回答2:


I wrote a simple template class which makes it easy to create and register a transformer for any class that implements NSSecureCoding. It works fine for me in iOS 12 and 13, at least in my simple test using UIColor as the transformable attribute.

To use it (using UIColor as an example):

// Make UIColor adopt ValueTransforming
extension UIColor: ValueTransforming {
  static var valueTransformerName: NSValueTransformerName { 
    .init("UIColorValueTransformer")
  }
}

// Register the transformer somewhere early in app startup.
NSSecureCodingValueTransformer<UIColor>.registerTransformer()

The name of the transformer to use in the Core Data model is UIColorValueTransformer.

import Foundation

public protocol ValueTransforming: NSSecureCoding {
  static var valueTransformerName: NSValueTransformerName { get }
}

public class NSSecureCodingValueTransformer<T: NSSecureCoding & NSObject>: ValueTransformer {
  public override class func transformedValueClass() -> AnyClass { T.self }
  public override class func allowsReverseTransformation() -> Bool { true }

  public override func transformedValue(_ value: Any?) -> Any? {
    guard let value = value as? T else { return nil }
    return try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
  }

  public override func reverseTransformedValue(_ value: Any?) -> Any? {
    guard let data = value as? NSData else { return nil }
    let result = try? NSKeyedUnarchiver.unarchivedObject(
      ofClass: T.self,
      from: data as Data
    )
    return result
  }

  /// Registers the transformer by calling `ValueTransformer.setValueTransformer(_:forName:)`.
  public static func registerTransformer() {
    let transformer = NSSecureCodingValueTransformer<T>()
    ValueTransformer.setValueTransformer(transformer, forName: T.valueTransformerName)
  }
}


来源:https://stackoverflow.com/questions/57304922/crash-when-adopting-nssecureunarchivefromdatatransformer-for-a-transformable-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!