Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. // MARK: - Core Data stack
  2.  
  3. lazy var persistentContainer: NSPersistentContainer = {
  4. /*
  5. The persistent container for the application. This implementation
  6. creates and returns a container, having loaded the store for the
  7. application to it. This property is optional since there are legitimate
  8. error conditions that could cause the creation of the store to fail.
  9. */
  10. let container = NSPersistentContainer(name: "coreDataTestForPreOS")
  11. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  12. if let error = error as NSError? {
  13. // Replace this implementation with code to handle the error appropriately.
  14. // 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.
  15.  
  16. /*
  17. Typical reasons for an error here include:
  18. * The parent directory does not exist, cannot be created, or disallows writing.
  19. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  20. * The device is out of space.
  21. * The store could not be migrated to the current model version.
  22. Check the error message to determine what the actual problem was.
  23. */
  24. fatalError("Unresolved error (error), (error.userInfo)")
  25. }
  26. })
  27. return container
  28. }()
  29.  
  30. // MARK: - Core Data Saving support
  31.  
  32. func saveContext () {
  33. let context = persistentContainer.viewContext
  34. if context.hasChanges {
  35. do {
  36. try context.save()
  37. } catch {
  38. // Replace this implementation with code to handle the error appropriately.
  39. // 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.
  40. let nserror = error as NSError
  41. fatalError("Unresolved error (nserror), (nserror.userInfo)")
  42. }
  43. }
  44. }
  45.  
  46. }
  47.  
  48. let ad = UIApplication.shared.delegate as! AppDelegate
  49. let context = ad.persistentContainer.viewContext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement