Last active
July 2, 2018 02:23
-
-
Save andrehsouza/ea1c14b81c1950e2160c6c3ca0f7bc8b to your computer and use it in GitHub Desktop.
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 UIKit | |
import CoreData | |
//MARK: - Core Data stack | |
class CoreDataStack: NSObject { | |
static let sharedInstance = CoreDataStack() | |
static let containerName = "CoreDataExample" //CoreDataExample.xcdatamodeld | |
private override init() {} | |
lazy var persistentContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: CoreDataStack.containerName) | |
container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
if let error = error as NSError? { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
}) | |
return container | |
}() | |
} | |
// MARK: - Core Data Saving support | |
extension CoreDataStack { | |
func saveContext() { | |
let context = persistentContainer.viewContext | |
if context.hasChanges { | |
do { | |
try context.save() | |
} catch { | |
let nserror = error as NSError | |
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment