Created
July 13, 2020 23:48
-
-
Save scottfinkelstein/0fb3ea2f496860e09d67401aade07ecc to your computer and use it in GitHub Desktop.
CoreData Singleton class
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
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