Last active
August 28, 2023 14:35
-
-
Save avdyushin/b67e4524edcfb1aec47605da1a4bea7a to your computer and use it in GitHub Desktop.
CoreData stack for iOS 9 and iOS 10 using Swift 3
This file contains hidden or 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
// | |
// Storage.swift | |
// | |
// Created by Grigory Avdyushin on 30.06.16. | |
// Copyright © 2016 Grigory Avdyushin. All rights reserved. | |
// | |
import UIKit | |
import CoreData | |
/// NSPersistentStoreCoordinator extension | |
extension NSPersistentStoreCoordinator { | |
/// NSPersistentStoreCoordinator error types | |
public enum CoordinatorError: Error { | |
/// .momd file not found | |
case modelFileNotFound | |
/// NSManagedObjectModel creation fail | |
case modelCreationError | |
/// Gettings document directory fail | |
case storePathNotFound | |
} | |
/// Return NSPersistentStoreCoordinator object | |
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 Storage { | |
static var shared = Storage() | |
@available(iOS 10.0, *) | |
private lazy var persistentContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: "Model") | |
container.loadPersistentStores { (storeDescription, error) in | |
print("CoreData: Inited \(storeDescription)") | |
guard error == nil else { | |
print("CoreData: Unresolved error \(error)") | |
return | |
} | |
} | |
return container | |
}() | |
private lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { | |
do { | |
return try NSPersistentStoreCoordinator.coordinator(name: "Model") | |
} 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 | |
} | |
} |
Hi, I am new on swift 3 and I need to implement this file. How I can init this file
Great
Excellent. Thanks!
Good job sir, thank you very much
Thanks! Great job.
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
}