comparison Simoleon/Persistence.swift @ 173:ad8c6567539d

restart project
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Tue, 26 Oct 2021 13:09:17 +0200
parents
children 5a9430fd6b4d
comparison
equal deleted inserted replaced
172:9b0486301fbf 173:ad8c6567539d
1 //
2 // Persistence.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 26/10/21.
6 //
7
8 import CoreData
9
10 struct PersistenceController {
11 static let shared = PersistenceController()
12
13 static var preview: PersistenceController = {
14 let result = PersistenceController(inMemory: true)
15 let viewContext = result.container.viewContext
16 for _ in 0..<10 {
17 let newItem = Item(context: viewContext)
18 newItem.timestamp = Date()
19 }
20 do {
21 try viewContext.save()
22 } catch {
23 // Replace this implementation with code to handle the error appropriately.
24 // 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.
25 let nsError = error as NSError
26 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
27 }
28 return result
29 }()
30
31 let container: NSPersistentCloudKitContainer
32
33 init(inMemory: Bool = false) {
34 container = NSPersistentCloudKitContainer(name: "Simoleon")
35 if inMemory {
36 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
37 }
38 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
39 if let error = error as NSError? {
40 // Replace this implementation with code to handle the error appropriately.
41 // 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.
42
43 /*
44 Typical reasons for an error here include:
45 * The parent directory does not exist, cannot be created, or disallows writing.
46 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
47 * The device is out of space.
48 * The store could not be migrated to the current model version.
49 Check the error message to determine what the actual problem was.
50 */
51 fatalError("Unresolved error \(error), \(error.userInfo)")
52 }
53 })
54 }
55 }