Mercurial > public > geoquiz
comparison GeoQuiz/Logic/PersistenceController.swift @ 20:e281791e0494
finish implementation
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 23 Oct 2022 11:48:39 +0100 |
parents | f140bb277c96 |
children | b145c408f791 |
comparison
equal
deleted
inserted
replaced
19:f140bb277c96 | 20:e281791e0494 |
---|---|
5 // Created by Dennis Concepción Martín on 19/10/22. | 5 // Created by Dennis Concepción Martín on 19/10/22. |
6 // | 6 // |
7 | 7 |
8 import CoreData | 8 import CoreData |
9 | 9 |
10 class PersistenceController: ObservableObject { | 10 class PersistenceController { |
11 let container: NSPersistentContainer | 11 static let shared = PersistenceController() |
12 | |
13 init(inMemory: Bool = false) { | |
14 container = NSPersistentContainer(name: "GeoQuiz") | |
15 | |
16 if inMemory { | |
17 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") | |
18 } | |
19 | |
20 container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
21 if let error = error { | |
22 fatalError("Unresolved error \(error.localizedDescription)") | |
23 } | |
24 }) | |
25 | |
26 container.viewContext.automaticallyMergesChangesFromParent = true | |
27 } | |
28 | 12 |
29 // Create mock data for previews | 13 // Create mock data for previews |
30 static var preview: PersistenceController = { | 14 static var preview: PersistenceController = { |
31 let result = PersistenceController(inMemory: true) | 15 let result = PersistenceController(inMemory: true) |
32 let viewContext = result.container.viewContext | 16 let viewContext = result.container.viewContext |
33 | 17 |
34 for _ in 0..<10 { | 18 for _ in 0..<10 { |
35 let playedGame = PlayedGame(context: viewContext) | 19 let playedGame = PlayedGame(context: viewContext) |
36 playedGame.id = UUID() | |
37 playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! | 20 playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! |
38 playedGame.score = Int32.random(in: 0...50) | 21 playedGame.score = Int32.random(in: 0...50) |
39 playedGame.date = Date() | 22 playedGame.date = Date() |
40 | 23 |
41 if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { | 24 if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { |
53 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | 36 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") |
54 } | 37 } |
55 | 38 |
56 return result | 39 return result |
57 }() | 40 }() |
41 | |
42 let container: NSPersistentCloudKitContainer | |
43 | |
44 init(inMemory: Bool = false) { | |
45 container = NSPersistentCloudKitContainer(name: "GeoQuiz") | |
46 | |
47 if inMemory { | |
48 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") | |
49 } | |
50 | |
51 container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
52 if let error = error as NSError? { | |
53 fatalError("Unresolved error \(error), \(error.userInfo)") | |
54 } | |
55 }) | |
56 | |
57 container.viewContext.automaticallyMergesChangesFromParent = true | |
58 } | |
58 } | 59 } |