Last active
February 15, 2023 16:43
-
-
Save alimovlex/e0db83d0a40d44243425e7e464fbd385 to your computer and use it in GitHub Desktop.
The coreData stack for iOS 9 and iOS 10
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
| /* | |
| * Copyright (C) 2023 Recompile.me. | |
| * All rights reserved. | |
| */ | |
| // MARK: - Core Data stack | |
| @available(iOS 10.0, *) | |
| lazy var persistentContainer: NSPersistentContainer = { | |
| //goalpost.xcdatamodeld The name of the CoreData model | |
| let container = NSPersistentContainer(name: "goalpost") | |
| container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
| if let error = error as NSError? { | |
| fatalError("Unresolved error \(error), \(error.userInfo)") | |
| } | |
| }) | |
| return container | |
| }() | |
| //iOS 9 coredata | |
| lazy var applicationDocumentsDirectory: URL = { | |
| let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | |
| return urls[urls.count-1] | |
| }() | |
| lazy var managedObjectModel: NSManagedObjectModel = { | |
| //goalpost.xcdatamodeld The name of the CoreData model | |
| let modelURL = Bundle.main.url(forResource: "goalpost", withExtension: "momd")! | |
| return NSManagedObjectModel(contentsOf: modelURL)! | |
| }() | |
| lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { | |
| let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) | |
| //goalpost.xcdatamodeld The name of the CoreData model | |
| let url = self.applicationDocumentsDirectory.appendingPathComponent("goalpost.sqlite") | |
| var failureReason = "There was an error creating or loading the application's saved data." | |
| do { | |
| try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil) | |
| } catch { | |
| var dict = [String: AnyObject]() | |
| dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject? | |
| dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject? | |
| dict[NSUnderlyingErrorKey] = error as NSError | |
| let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) | |
| NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") | |
| abort() | |
| } | |
| return coordinator | |
| }() | |
| lazy var managedObjectContext: NSManagedObjectContext = { | |
| let coordinator = self.persistentStoreCoordinator | |
| var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
| managedObjectContext.persistentStoreCoordinator = coordinator | |
| managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy | |
| return managedObjectContext | |
| }() | |
| // MARK: - Core Data Saving support | |
| func saveContext () { | |
| if #available(iOS 10.0, *) { | |
| let context = persistentContainer.viewContext | |
| if context.hasChanges { | |
| do { | |
| try context.save() | |
| } catch { | |
| let nserror = error as NSError | |
| fatalError("Unresolved error \(nserror), \(nserror.userInfo)") | |
| } | |
| } | |
| } else { | |
| if managedObjectContext.hasChanges { | |
| do { | |
| try managedObjectContext.save() | |
| } catch { | |
| let nserror = error as NSError | |
| NSLog("Unresolved error \(nserror), \(nserror.userInfo)") | |
| abort() | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment