Skip to content

Instantly share code, notes, and snippets.

@Prakhar896
Last active August 28, 2023 13:55
Show Gist options
  • Save Prakhar896/f04d0d62d3f287bd037ced590535a612 to your computer and use it in GitHub Desktop.
Save Prakhar896/f04d0d62d3f287bd037ced590535a612 to your computer and use it in GitHub Desktop.
Common Core Data persistent store loading controller that could be implemented in any app using Core Data.
class DataController: ObservableObject {
let container = NSPersistentContainer(name: <#Name#>)
init() {
container.loadPersistentStores { description, error in
if let error = error {
print("Core Data failed to load: \(error.localizedDescription)")
}
// self.container.viewContext.mergePolicy = <#NSMergePolicy#>
}
}
}
// Sample Implementation in SwiftUI Top-Level App File:
//struct MyApp: App {
// @StateObject private var dataController = DataController()
//
// var body: some Scene {
// WindowGroup {
// ContentView()
// .environment(\.managedObjectContext, dataController.container.viewContext)
// }
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment