5 // Created by Ruben Beltran del Rio on 2/1/21.
10 struct PersistenceController {
11 static let shared = PersistenceController()
13 static var preview: PersistenceController = {
14 let result = PersistenceController(inMemory: true)
15 let viewContext = result.container.viewContext
17 let newMap = Map(context: viewContext)
19 newMap.createdAt = Date()
20 newMap.title = "Map \(newMap.createdAt!.format())"
24 try viewContext.save()
26 // Replace this implementation with code to handle the error appropriately.
27 // 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.
28 let nsError = error as NSError
29 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
34 let container: NSPersistentCloudKitContainer
36 init(inMemory: Bool = false) {
37 container = NSPersistentCloudKitContainer(name: "Map")
39 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
41 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
42 if let error = error as NSError? {
43 // Replace this implementation with code to handle the error appropriately.
44 // 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.
47 Typical reasons for an error here include:
48 * The parent directory does not exist, cannot be created, or disallows writing.
49 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
50 * The device is out of space.
51 * The store could not be migrated to the current model version.
52 Check the error message to determine what the actual problem was.
54 fatalError("Unresolved error \(error), \(error.userInfo)")