]> git.r.bdr.sh - rbdr/captura/blob - Captura/Data/Persistence.swift
4490e38cad1aa391df800d2e5a6bf675205df9a0
[rbdr/captura] / Captura / Data / Persistence.swift
1 import CoreData
2
3 struct PersistenceController {
4 static let shared = PersistenceController()
5
6 static var preview: PersistenceController = {
7 let result = PersistenceController(inMemory: true)
8 return result
9 }()
10
11 let container: NSPersistentCloudKitContainer
12
13 init(inMemory: Bool = false) {
14 container = NSPersistentCloudKitContainer(name: "Captura")
15
16 #if DEBUG
17 do {
18 // Use the container to initialize the development schema.
19 try container.initializeCloudKitSchema(options: [])
20 } catch {
21 // Handle any errors.
22 }
23 #endif
24
25 if inMemory {
26 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
27 }
28 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
29 if let error = error as NSError? {
30 // Replace this implementation with code to handle the error appropriately.
31 // 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.
32
33 /*
34 Typical reasons for an error here include:
35 * The parent directory does not exist, cannot be created, or disallows writing.
36 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
37 * The device is out of space.
38 * The store could not be migrated to the current model version.
39 Check the error message to determine what the actual problem was.
40 */
41 fatalError("Unresolved error \(error), \(error.userInfo)")
42 }
43 })
44 container.viewContext.automaticallyMergesChangesFromParent = true
45 }
46 }