How to use ManagedObjectContext in non View Swift file

233 Views Asked by At

I use standard approach for injection of CoreData ManagedObjectContect from App struct

WindowGroup {
        ContentView()
            .environment(\.managedObjectContext, persistenceController.container.viewContext)
    }

to all my views via wrapper

 @Environment(\.managedObjectContext) private var viewContext

and it works fine. I'd like to create class with methods handling CoreData objects in the same context but @Environment wrapper used as above doesn't work in non SfiftUI file. What is recommended approach here to handle CoreData?

0

There are 0 best solutions below