Skip to content

Instantly share code, notes, and snippets.

@marcos1262
Created October 27, 2018 13:43
Show Gist options
  • Save marcos1262/facd378ee6b294929fc10ba0dfd2560b to your computer and use it in GitHub Desktop.
Save marcos1262/facd378ee6b294929fc10ba0dfd2560b to your computer and use it in GitHub Desktop.
Wrapped CoreData stack
//
// DataManager.swift
//
// Created by Marcos Santos on 8/30/18.
// Copyright © 2018 Marcos Santos 2018. All rights reserved.
//
import CoreData
public class DataManager {
public static var shared = DataManager()
public var bundle: Bundle?
public var modelName: String?
private init(){}
lazy public var persistentContainer: NSPersistentContainer? = {
guard let bundle = self.bundle else{
if let modelName = self.modelName{
return loadContainer(modelName: modelName)
}
else{
return loadContainer()
}
}
guard let modelName = self.modelName else{
return loadContainer(bundle: bundle)
}
return loadContainer(bundle: bundle, modelName: modelName)
}()
public class func getContext() throws -> NSManagedObjectContext {
if let context = shared.persistentContainer?.viewContext {
return context
}
throw Errors.wrongContext
}
public func loadContainer(bundle: Bundle = Bundle(for: DataManager.self), modelName: String = "ModelName") -> NSPersistentContainer? {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
guard let modelURL = bundle.url(forResource: modelName, withExtension:"momd"), let model = NSManagedObjectModel(contentsOf: modelURL) else {
print("Error loading model from bundle")
return nil
}
var container: NSPersistentContainer? = NSPersistentContainer(name: modelName, managedObjectModel: model)
container?.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
print("Unresolved error \(error), \(error.userInfo)")
container = nil
}
})
return container
}
// MARK: - Core Data Saving support
public func saveContext() throws {
guard let context = persistentContainer?.viewContext else{
throw Errors.wrongContainer
}
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
throw Errors.cantSave
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment