comparison Simoleon/Persistence.swift @ 55:ecc58b9cd89d

Added WatchOS target
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Tue, 27 Jul 2021 18:05:28 +0100
parents b0bce2c8e4a9
children
comparison
equal deleted inserted replaced
54:dbd3fe16efaa 55:ecc58b9cd89d
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 {
17 let newItem = Item(context: viewContext)
18 newItem.timestamp = Date()
19 }
20 16
21 for _ in 0..<10 { 17 for _ in 0..<10 {
22 let favorite = Favorite(context: viewContext) 18 let favorite = Favorite(context: viewContext)
23 favorite.currencyPair = "USD/GBP" 19 favorite.currencyPair = "USD/GBP"
24 } 20 }
25 do { 21 do {
26 try viewContext.save() 22 try viewContext.save()
27 } catch { 23 } catch {
28 // Replace this implementation with code to handle the error appropriately. 24 /*
29 // 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 Replace this implementation with code to handle the error appropriately.
26 fatalError() causes the application to generate a crash log and terminate.
27 You should not use this function in a shipping application, although it may be useful during development.
28 */
30 let nsError = error as NSError 29 let nsError = error as NSError
31 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 30 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
32 } 31 }
33 return result 32 return result
34 }() 33 }()
42 if inMemory { 41 if inMemory {
43 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") 42 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
44 } 43 }
45 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 44 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
46 if let error = error as NSError? { 45 if let error = error as NSError? {
47 // Replace this implementation with code to handle the error appropriately. 46 /*
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. 47 Replace this implementation with code to handle the error appropriately.
48 fatalError() causes the application to generate a crash log and terminate.
49 You should not use this function in a shipping application, although it may be useful during development.
50 */
49 51
50 /* 52 /*
51 Typical reasons for an error here include: 53 Typical reasons for an error here include:
52 * The parent directory does not exist, cannot be created, or disallows writing. 54 * 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. 55 * The persistent store is not accessible, due to permissions or data protection when the device is locked.