No Description

Persistence.swift 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // Persistence.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 10/24/20.
  6. //
  7. import CoreData
  8. struct PersistenceController {
  9. static let shared = PersistenceController()
  10. static var preview: PersistenceController = {
  11. let result = PersistenceController(inMemory: true)
  12. let viewContext = result.container.viewContext
  13. for _ in 0..<10 {
  14. let newItem = Item(context: viewContext)
  15. newItem.timestamp = Date()
  16. }
  17. do {
  18. try viewContext.save()
  19. } catch {
  20. // Replace this implementation with code to handle the error appropriately.
  21. // 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.
  22. let nsError = error as NSError
  23. fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
  24. }
  25. return result
  26. }()
  27. let container: NSPersistentCloudKitContainer
  28. init(inMemory: Bool = false) {
  29. container = NSPersistentCloudKitContainer(name: "Comedores_Sociales")
  30. if inMemory {
  31. container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
  32. }
  33. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  34. if let error = error as NSError? {
  35. // Replace this implementation with code to handle the error appropriately.
  36. // 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.
  37. /*
  38. Typical reasons for an error here include:
  39. * The parent directory does not exist, cannot be created, or disallows writing.
  40. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  41. * The device is out of space.
  42. * The store could not be migrated to the current model version.
  43. Check the error message to determine what the actual problem was.
  44. */
  45. fatalError("Unresolved error \(error), \(error.userInfo)")
  46. }
  47. })
  48. }
  49. }