title: Error with retrieve / save custom object in core data
body: <p>I have developed an ios application that allow users to edit a musical score sheet and now i'd like to implement data persistence to prevent the discarding of changes.</p>
  <p>Reading on ios documentation i've noticed that exists different ways to improve data persistence and I believe that the best way for my application is Core Data.
  Considering that my application use a lot of custom object i met a lot of problems.</p>
  <p>I'm trying to use core data to save an entity, referred to a score sheet, composed by two attributes:</p>
  <ul>
  <li>name: String</li>
  <li>score: Array of another custom object (Measure), composed by other custom object (Score Element)</li>
  </ul>
  <p>According to documentation and other q/a I've decided to use a Trasformable type on the model:</p>
  <p><a href="https://i.stack.imgur.com/e10x7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e10x7.png" alt="enter image description here" /></a></p>
  <p>So I've declared a generic class used as trasformer for score attribute:</p>
  <pre><code>public class NSSecureCodingValueTransformer&lt;T: NSSecureCoding &amp; NSObject&gt;: ValueTransformer {
    public override class func transformedValueClass() -&gt; AnyClass { T.self }
    public override class func allowsReverseTransformation() -&gt; Bool { true }
  
    public override func transformedValue(_ value: Any?) -&gt; Any? {
      guard let value = value as? T else { return nil }
      return try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
    }
  
    public override func reverseTransformedValue(_ value: Any?) -&gt; Any? {
      guard let data = value as? NSData else { return nil }
      let result = try? NSKeyedUnarchiver.unarchivedObject(
        ofClass: T.self,
        from: data as Data
      )
      return result
    }
  
    /// The name of this transformer. This is the name used to register the transformer using `ValueTransformer.setValueTransformer(_:forName:)`
    public static var transformerName: NSValueTransformerName {
      let className = NSStringFromClass(T.self)
      return NSValueTransformerName(&quot;DHC\(className)ValueTransformer&quot;)
    }
  
    /// Registers the transformer by calling `ValueTransformer.setValueTransformer(_:forName:)`.
    public static func registerTransformer() {
      let transformer = NSSecureCodingValueTransformer&lt;T&gt;()
      ValueTransformer.setValueTransformer(transformer, forName: transformerName)
    }
  }
  </code></pre>
  <p>Using in this way a DHCMeasureValueTransformer as trasformer in DataModel file.
  The problem is that when i save, no error occurs but when i fetch data for a new restart of application, i can fetch just the name of score sheet, while the score array it's empty, like if no elements it's been put inside (clearly, before of save, i've try to print array content, that prove that i'm working with a non empty array)</p>
  <p>Here is the code of the save:</p>
  <pre><code>static func saveContext() {
          let context = getContext()
          do {
              try context.save()
          } catch {
              print(&quot;error during the save.&quot;)
          }
      }
  </code></pre>
  <p>And here is the code of two classes of the entity object:</p>
  <pre><code>
  // DataClass
  @objc(ScoreSheet)
  public class ScoreSheet: NSManagedObject {
      static var supportsSecureCoding: Bool {
          return true
      }
  }
  
  //DataProperties
  extension ScoreSheet {
  
      @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;ScoreSheet&gt; {
          return NSFetchRequest&lt;ScoreSheet&gt;(entityName: &quot;ScoreSheet&quot;)
      }
  
      @NSManaged public var name: String
      @NSManaged public var score: [Measure]
  }
  
  </code></pre>
  <p>Clearly Measure class implements NSSecureCoding and method for decode and encode the object.</p>
  <p>Here is Measure class implementation:</p>
  <pre><code>
  import Foundation
  
  
  class Measure: NSObject, NSCoding, NSSecureCoding {
      
      var elements : [ScoreElement] = []
      var timeSig : TimeSignature
      var clef : Clef
      static var supportsSecureCoding = true
      
      init(time : TimeSignature, clef : Clef) {
        self.timeSig = time
        self.clef = clef
      }
      
      func encode(with encoder: NSCoder) {
          encoder.encode(self.elements, forKey: &quot;elements&quot;)
          encoder.encode(self.timeSig, forKey: &quot;timeSig&quot;)
          encoder.encode(self.clef, forKey: &quot;clef&quot;)
          
      }
      
      required convenience init? (coder decoder: NSCoder) {
          let elements = decoder.decodeObject(forKey: &quot;elements&quot;) as! [ScoreElement]
          let timeSig = decoder.decodeObject(forKey: &quot;timeSig&quot;) as! TimeSignature
          let clef = decoder.decodeObject(forKey: &quot;clef&quot;) as! Clef
          
          self.init(time: timeSig, clef: clef)
          self.elements = elements
      }
      
    
  }
  
  </code></pre>