Created
December 4, 2018 03:31
-
-
Save thepost/97703dab628e5e7975539f2171ce555a 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 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