I am working with the Photos
and CoreML
frameworks in Swift to implement some image analysis functionality in an App I'm working on. I require the ability to store the result of VNGenerateImageFeaturePrintRequest()
, which has type VNFeaturePrintObservation
.
These results need to be persisted for each image so that they can be used later without re-calculating, as it is a slow process. VNFeaturePrintObservation
doesn't conform to Codable
, so I can't use something like a JSONEncoder
which is how I've previously persisted data like this into Core Data.
Is there a way I can persist my VNFeaturePrintObservation
objects in Core Data?
I'm not familiar with VNFeaturePrintObservation
, but the docs say that
VNObservation
, andVNObservation
conforms to NSSecureCoding
That means that VNFeaturePrintObservation
also conforms to NSSecureCoding
. Any class that conforms to NSSecureCoding
can be saved in Core Data by using a Core Data attribute with the type set to "Transformable". Thanks to NSSecureCoding
there's no additional work.
That would look like this in the Core Data model editor:
Two things to notice:
VNFeaturePrintObservation
If you have Xcode generate your source code, the property will look like this:
@NSManaged public var observation: VNFeaturePrintObservation?
You don't have to do anything special to use this property.
Incidentally, Codable
doesn't apply here, because Core Data doesn't use Codable
. NSSecureCoding
will convert to/from Data
automatically, and it's what you need to use with Core Data.