SHARE
TWEET

Untitled

a guest Oct 19th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CoreDataStack {
  2.    
  3.     static var applicationDocumentsDirectory: URL = {
  4.        
  5.         let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
  6.         return urls[urls.count-1]
  7.     }()
  8.    
  9.     static var managedObjectModel: NSManagedObjectModel = {
  10.         /**
  11.          The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
  12.          */
  13.         //
  14.         let modelURL = Bundle(for: CoreDataStack.self).url(forResource: "storio", withExtension: "momd")!
  15.         /**
  16.          type your database name here..
  17.          */// type your database name here..
  18.         return NSManagedObjectModel(contentsOf: modelURL)!
  19.     }()
  20.    
  21.     static var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
  22.         /**
  23.          The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added 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.
  24.          // Create the coordinator and store
  25.          */
  26.         // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added 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.
  27.         // Create the coordinator and store
  28.         let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
  29.         let url = applicationDocumentsDirectory.appendingPathComponent("storio") // type your database name here...
  30.         var failureReason = "There was an error creating or loading the application's saved data."
  31.         let options = [NSMigratePersistentStoresAutomaticallyOption: NSNumber(value: true as Bool), NSInferMappingModelAutomaticallyOption: NSNumber(value: true as Bool)]
  32.         do {
  33.             try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
  34.         } catch {
  35.             // Report any error we got.
  36.             var dict = [String: AnyObject]()
  37.             dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject
  38.             dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject
  39.            
  40.             dict[NSUnderlyingErrorKey] = error as NSError
  41.             let wrappedError = NSError(domain: "storio", code: 9999, userInfo: dict)
  42.             // Replace this with code to handle the error appropriately.
  43.             // abort() 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.
  44.             NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
  45.             abort()
  46.         }
  47.        
  48.         return coordinator
  49.     }()
  50.    
  51.     static var managedObjectContext: NSManagedObjectContext = {
  52.         // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
  53.         let coordinator = persistentStoreCoordinator
  54.         var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  55.         managedObjectContext.persistentStoreCoordinator = coordinator
  56.         return managedObjectContext
  57.     }()
  58.    
  59.     // MARK: - Core Data Saving support
  60.    
  61.     static func saveContext () {
  62.         if managedObjectContext.hasChanges {
  63.             do {
  64.                 try managedObjectContext.save()
  65.             } catch {
  66.                 // Replace this implementation with code to handle the error appropriately.
  67.                 // abort() 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.
  68.                 let nserror = error as NSError
  69.                 NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
  70.                 abort()
  71.             }
  72.         }
  73.     }
  74.    
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top