comparison Simoleon/Persistence.swift @ 47:75c1a05176f6

Refactor code
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 26 Jul 2021 20:08:20 +0100
parents 933d9ab04374
children b0bce2c8e4a9
comparison
equal deleted inserted replaced
46:ce4eb7416b41 47:75c1a05176f6
7 7
8 import CoreData 8 import CoreData
9 9
10 struct PersistenceController { 10 struct PersistenceController {
11 static let shared = PersistenceController() 11 static let shared = PersistenceController()
12 12
13 static var preview: PersistenceController = { 13 static var preview: PersistenceController = {
14 let result = PersistenceController(inMemory: true) 14 let result = PersistenceController(inMemory: true)
15 let viewContext = result.container.viewContext 15 let viewContext = result.container.viewContext
16 for _ in 0..<10 { 16 for _ in 0..<10 {
17 let newItem = Item(context: viewContext) 17 let newItem = Item(context: viewContext)
30 let nsError = error as NSError 30 let nsError = error as NSError
31 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 31 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
32 } 32 }
33 return result 33 return result
34 }() 34 }()
35 35
36 let container: NSPersistentCloudKitContainer 36 let container: NSPersistentCloudKitContainer
37 37
38 init(inMemory: Bool = false) { 38 init(inMemory: Bool = false) {
39 container = NSPersistentCloudKitContainer(name: "Simoleon") 39 container = NSPersistentCloudKitContainer(name: "Simoleon")
40 container.viewContext.automaticallyMergesChangesFromParent = true 40 container.viewContext.automaticallyMergesChangesFromParent = true
41 container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy 41 container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
42 if inMemory { 42 if inMemory {
44 } 44 }
45 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 45 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
46 if let error = error as NSError? { 46 if let error = error as NSError? {
47 // Replace this implementation with code to handle the error appropriately. 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. 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 49
50 /* 50 /*
51 Typical reasons for an error here include: 51 Typical reasons for an error here include:
52 * The parent directory does not exist, cannot be created, or disallows writing. 52 * The parent directory does not exist, cannot be created, or disallows writing.
53 * The persistent store is not accessible, due to permissions or data protection when the device is locked. 53 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
54 * The device is out of space. 54 * The device is out of space.
55 * The store could not be migrated to the current model version. 55 * The store could not be migrated to the current model version.
56 Check the error message to determine what the actual problem was. 56 Check the error message to determine what the actual problem was.
57 */ 57 */
58 fatalError("Unresolved error \(error), \(error.userInfo)") 58 fatalError("Unresolved error \(error), \(error.userInfo)")
59 } 59 }
60 }) 60 })
61 } 61 }
62 } 62 }