core-dataswiftuinsvaluetransformer

In a SwiftUI lifecycle app, where exactly should I register a CoreData transformerValue?


I'm trying to follow this tutorial:

https://www.avanderlee.com/swift/valuetransformer-core-data/

But I'm stumped on where (and how!) exactly to use

UIColorValueTransformer.register()

The author says to do it before setting up the persistent container... and I'm think I'm doing that here:

import SwiftUI

@main
struct ContactApp: App {
    
    let persistenceController = PersistenceController.shared

    var body: some Scene {
        WindowGroup {
            MainTabView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
    }
}

But I'm at a loss of what exactly to write, or if this is the right place. Maybe I should be doing it in a different place?


Solution

  • Put it into init, like

    class PersistenceController {
       static let shared = PersistenceController()
    
       init() {
          UIColorValueTransformer.register()       // << here !!
      
          // ... other init code
       }
    
       // ... other code
    }