Sin descripción

AppDelegate.swift 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // AppDelegate.swift
  3. // EncuestaMarle
  4. //
  5. // Created by Tatiana Castro on 4/13/19.
  6. // Copyright © 2019 Marle. All rights reserved.
  7. //
  8. import UIKit
  9. import CoreData
  10. @UIApplicationMain
  11. class AppDelegate: UIResponder, UIApplicationDelegate {
  12. var window: UIWindow?
  13. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  14. // Override point for customization after application launch.
  15. return true
  16. }
  17. func applicationWillResignActive(_ application: UIApplication) {
  18. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  19. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  20. }
  21. func applicationDidEnterBackground(_ application: UIApplication) {
  22. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  23. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  24. }
  25. func applicationWillEnterForeground(_ application: UIApplication) {
  26. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  27. }
  28. func applicationDidBecomeActive(_ application: UIApplication) {
  29. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  30. }
  31. func applicationWillTerminate(_ application: UIApplication) {
  32. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  33. // Saves changes in the application's managed object context before the application terminates.
  34. self.saveContext()
  35. }
  36. // MARK: - Core Data stack
  37. lazy var persistentContainer: NSPersistentContainer = {
  38. /*
  39. The persistent container for the application. This implementation
  40. creates and returns a container, having loaded the store for the
  41. application to it. This property is optional since there are legitimate
  42. error conditions that could cause the creation of the store to fail.
  43. */
  44. let container = NSPersistentContainer(name: "EncuestaMarle")
  45. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  46. if let error = error as NSError? {
  47. // Replace this implementation with code to handle the error appropriately.
  48. // 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.
  49. /*
  50. Typical reasons for an error here include:
  51. * The parent directory does not exist, cannot be created, or disallows writing.
  52. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  53. * The device is out of space.
  54. * The store could not be migrated to the current model version.
  55. Check the error message to determine what the actual problem was.
  56. */
  57. fatalError("Unresolved error \(error), \(error.userInfo)")
  58. }
  59. })
  60. return container
  61. }()
  62. // MARK: - Core Data Saving support
  63. func saveContext () {
  64. let context = persistentContainer.viewContext
  65. if context.hasChanges {
  66. do {
  67. try context.save()
  68. } catch {
  69. // Replace this implementation with code to handle the error appropriately.
  70. // 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.
  71. let nserror = error as NSError
  72. fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  73. }
  74. }
  75. }
  76. }