Last active
August 28, 2023 13:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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