Skip to content

Instantly share code, notes, and snippets.

@thepost
Created December 4, 2018 03:31
Show Gist options
  • Save thepost/97703dab628e5e7975539f2171ce555a to your computer and use it in GitHub Desktop.
Save thepost/97703dab628e5e7975539f2171ce555a to your computer and use it in GitHub Desktop.
import CoreData
public class ModelController {
typealias VoidCompletion = () -> ()
// MARK: - Properties
private var modelName: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: modelName)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
lazy var context: NSManagedObjectContext = {
return persistentContainer.viewContext
}()
lazy var backgroundMOC: NSManagedObjectContext = {
return persistentContainer.newBackgroundContext()
}()
// MARK: - Convenience Init
convenience init(modelName model: String) {
self.init()
modelName = model
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment