title: CoreData - Store an array of Tranformable objects
body: <p>I have an entity that has a traformable property. It is an array of custom object, <code>Reminder</code> that confirms to <code>NSSecureCoding</code>.</p>
  <pre><code>@objc(Reminder)
  public class Reminder: NSObject, NSSecureCoding {
      public static var supportsSecureCoding: Bool = true
      
      
      public var date: Date
      public var isOn: Bool
      
      public  init(date: Date, isOn: Bool) {
          self.date = date
          self.isOn = isOn
      }
      
      struct Keys {
          static var date: String = &quot;date&quot;
          static let isOn: String = &quot;isOn&quot;
      }
      
      public func encode(with aCoder: NSCoder) {
          aCoder.encode(date as NSDate,forKey: Keys.date)
          aCoder.encode(isOn,forKey: Keys.isOn)
      }
      
      required public init?(coder aDecoder: NSCoder) {
          guard let date = aDecoder.decodeObject(of: NSDate.self, forKey: Keys.date) as Date? else {
              return nil
          }
          
          self.date = date
          self.isOn = aDecoder.decodeBool(forKey: Keys.isOn)
      }
  }
  </code></pre>
  <hr />
  <p>And the following code is my <code>NSSecureUnarchiveFromDataTransformer</code>.</p>
  <pre><code>class ReminderDataTransformer: NSSecureUnarchiveFromDataTransformer {
  
      override class func allowsReverseTransformation() -&gt; Bool {
          return true
      }
  
      override class func transformedValueClass() -&gt; AnyClass {
          return Reminder.self
      }
  
      override class var allowedTopLevelClasses: [AnyClass] {
          return [Reminder.self]
      }
  
      override func transformedValue(_ value: Any?) -&gt; Any? {
          guard let data = value as? Data else {
              fatalError(&quot;Wrong data type: value must be a Data object; received \(type(of: value))&quot;)
          }
          return super.transformedValue(data)
      }
  
      override func reverseTransformedValue(_ value: Any?) -&gt; Any? {
          guard let reminder = value as? [Reminder] else {
              fatalError(&quot;Wrong data type: value must be a Reminder object; received \(type(of: value))&quot;)
          }
          return super.reverseTransformedValue(reminder)
      }
  }
  
  
  
  extension NSValueTransformerName {
      static let reminderToDataTransformer = NSValueTransformerName(rawValue: &quot;ReminderToDataTransformer&quot;)
  }
  </code></pre>
  <hr />
  <p>I have registered <code>ReminderDataTransformer</code> using the following code before initializing <code>NSPersistantContainer</code>.</p>
  <pre><code>ValueTransformer.setValueTransformer(ReminderDataTransformer(), forName: .reminderToDataTransformer)
  </code></pre>
  <hr />
  <p>I have used <code>ReminderToDataTransformer</code> as Transformer in xCode's Data Model Inspector.</p>
  <hr />
  <p>But it does not work because of the following error that occurs when saving entities.</p>
  <pre><code> [error] error: SQLCore dispatchRequest: exception handling request: &lt;NSSQLSaveChangesRequestContext: 0x282ec0780&gt; , &lt;shared NSSecureUnarchiveFromData transformer&gt; threw while encoding a value. with userInfo of (null)
  CoreData: error: SQLCore dispatchRequest: exception handling request: &lt;NSSQLSaveChangesRequestContext: 0x282ec0780&gt; , &lt;shared NSSecureUnarchiveFromData transformer&gt; threw while encoding a value. with userInfo of (null)
  2020-12-31 21:44:09.300394+0100 ReminderApp[26406:6247995] [error] error: -executeRequest: encountered exception = &lt;shared NSSecureUnarchiveFromData transformer&gt; threw while encoding a value. with userInfo = (null)
  </code></pre>
  <h3>Update</h3>
  <p>There is no exception. When launching the app for the second time,  this error is logged in console.</p>
  <pre><code>[error] fault: exception raised during multi-threaded fetch &lt;shared NSSecureUnarchiveFromData transformer&gt; threw while decoding a value. ({
      NSUnderlyingError = &quot;Error Domain=NSCocoaErrorDomain Code=4864 \&quot;value for key 'root' was of unexpected class 'NSArray (0x1fa392238) [/System/Library/Frameworks/CoreFoundation.framework]'. Allowed classes are '{(\n    \&quot;Reminder (0x100fb6920) [/private/var/containers/Bundle/Application/306C3F0B-75AA-4A2D-A934-260B2EB63313/ReminderApp]\”\n)}’.\” UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'NSArray (0x1fa392238) [/System/Library/Frameworks/CoreFoundation.framework]'. 
  </code></pre>
  <hr />
  <p>I guess I am unable to correctly encode / decode Reminder's array as It works if I change the code to store <code>Reminder</code> instead of <code>[Reminder]</code>.</p>
  <p>Just to be clear, I can store <code>Reminder</code>, but not <code>[Reminder]</code>.</p>
  <hr />
  <p>How to store <code>[Reminder]</code> as <code>Transformable</code>?</p>
  <hr />