Skip to content

Instantly share code, notes, and snippets.

@scottfinkelstein
Created July 13, 2020 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottfinkelstein/0fb3ea2f496860e09d67401aade07ecc to your computer and use it in GitHub Desktop.
Save scottfinkelstein/0fb3ea2f496860e09d67401aade07ecc to your computer and use it in GitHub Desktop.
CoreData Singleton class
import Foundation
import CoreData
class DataStore {
static let shared = DataStore()
let persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "MyDataModel")
container.loadPersistentStores { description, error in
if let error = error {
print(error)
}
}
return container
}()
private init() {
}
public func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
}catch {
print(error)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment