Created
October 31, 2016 21:10
-
-
Save joamafer/a146912a46d1205318c71416c68dc4df to your computer and use it in GitHub Desktop.
CoreDataStack for iOS 9 and 10. Supporting Swift 3.0
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
// | |
// CoreDataStack.swift | |
// Jose | |
// | |
// Created by JOSE ANTONIO MARTINEZ FERNANDEZ on 31/10/2016. | |
// Copyright © 2016 joamafer. All rights reserved. | |
// | |
import UIKit | |
import CoreData | |
extension NSPersistentStoreCoordinator { | |
public enum CoordinatorError: Error { | |
case modelFileNotFound, modelCreationError, storePathNotFound | |
} | |
static func coordinator(name: String) throws -> NSPersistentStoreCoordinator? { | |
guard let modelURL = Bundle.main.url(forResource: name, withExtension: "momd") else { | |
throw CoordinatorError.modelFileNotFound | |
} | |
guard let model = NSManagedObjectModel(contentsOf: modelURL) else { | |
throw CoordinatorError.modelCreationError | |
} | |
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model) | |
guard let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { | |
throw CoordinatorError.storePathNotFound | |
} | |
do { | |
let url = documents.appendingPathComponent("\(name).sqlite") | |
let options = [ NSMigratePersistentStoresAutomaticallyOption : true, | |
NSInferMappingModelAutomaticallyOption : true ] | |
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options) | |
} catch { | |
throw error | |
} | |
return coordinator | |
} | |
} | |
struct CoreDataStack { | |
static var shared = CoreDataStack() | |
static let modelName = "CoreDataStack" | |
@available(iOS 10.0, *) | |
private lazy var persistentContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: modelName) | |
container.loadPersistentStores { (storeDescription, error) in | |
guard error == nil else { | |
print("CoreData: Unresolved error \(error)") | |
return | |
} | |
} | |
return container | |
}() | |
private lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { | |
do { | |
return try NSPersistentStoreCoordinator.coordinator(name: modelName) | |
} catch { | |
print("CoreData: Unresolved error \(error)") | |
} | |
return nil | |
}() | |
private lazy var managedObjectContext: NSManagedObjectContext = { | |
let coordinator = self.persistentStoreCoordinator | |
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
managedObjectContext.persistentStoreCoordinator = coordinator | |
return managedObjectContext | |
}() | |
// MARK: Public methods | |
enum SaveStatus { | |
case saved, rolledBack, hasNoChanges | |
} | |
var context: NSManagedObjectContext { | |
mutating get { | |
if #available(iOS 10.0, *) { | |
return persistentContainer.viewContext | |
} else { | |
return managedObjectContext | |
} | |
} | |
} | |
mutating func save() -> SaveStatus { | |
if context.hasChanges { | |
do { | |
try context.save() | |
return .saved | |
} catch { | |
context.rollback() | |
return .rolledBack | |
} | |
} | |
return .hasNoChanges | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, how to access app group container with core data stack for ios 10... with
static let applicationGroupIdentifier = "com.fetchDatainExtension"
if let newURL =
FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: "group.com.fetchDatainExtension.contacts") {
url = newURL
}