Mercurial > public > lazybear
comparison LazyBear/Persistence.swift @ 463:783b567800d9
Starts new version
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 17 Jul 2021 09:33:26 +0100 |
parents | 4effac4733b0 |
children | 6953d83060a4 |
comparison
equal
deleted
inserted
replaced
462:daeac7c7c586 | 463:783b567800d9 |
---|---|
1 // | 1 // |
2 // Persistence.swift | 2 // Persistence.swift |
3 // LazyBear | 3 // lazybear |
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 17/2/21. | 5 // Created by Dennis Concepción Martín on 17/07/2021. |
6 // | 6 // |
7 | 7 |
8 import CoreData | 8 import CoreData |
9 | 9 |
10 struct PersistenceController { | 10 struct 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 entity = Entity(context: viewContext) | 17 let newItem = Item(context: viewContext) |
18 entity.attribute = 1 | 18 newItem.timestamp = Date() |
19 } | |
20 | |
21 for _ in 0..<10 { | |
22 let watchlistCompany = WatchlistCompany(context: viewContext) | |
23 watchlistCompany.name = "Apple Inc" | |
24 watchlistCompany.symbol = "AAPL" | |
25 watchlistCompany.watchlistName = "Technologies" | |
26 } | 19 } |
27 do { | 20 do { |
28 try viewContext.save() | 21 try viewContext.save() |
29 } catch { | 22 } catch { |
30 // Replace this implementation with code to handle the error appropriately. | 23 // Replace this implementation with code to handle the error appropriately. |
36 }() | 29 }() |
37 | 30 |
38 let container: NSPersistentCloudKitContainer | 31 let container: NSPersistentCloudKitContainer |
39 | 32 |
40 init(inMemory: Bool = false) { | 33 init(inMemory: Bool = false) { |
41 container = NSPersistentCloudKitContainer(name: "LazyBear") | 34 container = NSPersistentCloudKitContainer(name: "lazybear") |
42 container.viewContext.automaticallyMergesChangesFromParent = true // Not sure about this | |
43 if inMemory { | 35 if inMemory { |
44 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") | 36 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") |
45 } | 37 } |
46 container.loadPersistentStores(completionHandler: { (storeDescription, error) in | 38 container.loadPersistentStores(completionHandler: { (storeDescription, error) in |
47 if let error = error as NSError? { | 39 if let error = error as NSError? { |