Mercurial > public > geoquiz
comparison GeoQuiz/Logic/PersistenceController.swift @ 22:56add5561261
add mock data creation
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Wed, 26 Oct 2022 08:20:19 +0200 |
parents | b145c408f791 |
children | 02dcebb8cc4a |
comparison
equal
deleted
inserted
replaced
21:b145c408f791 | 22:56add5561261 |
---|---|
14 let container: NSPersistentCloudKitContainer | 14 let container: NSPersistentCloudKitContainer |
15 | 15 |
16 static var preview: PersistenceController = { | 16 static var preview: PersistenceController = { |
17 let result = PersistenceController(inMemory: true) | 17 let result = PersistenceController(inMemory: true) |
18 let viewContext = result.container.viewContext | 18 let viewContext = result.container.viewContext |
19 | |
20 #if DEBUG | |
19 createMockData(nil, viewContext) | 21 createMockData(nil, viewContext) |
22 #endif | |
20 | 23 |
21 return result | 24 return result |
22 }() | 25 }() |
23 | 26 |
24 init(inMemory: Bool = false) { | 27 init(inMemory: Bool = false) { |
42 for _ in 0..<10 { | 45 for _ in 0..<10 { |
43 let playedGame = PlayedGame(context: moc) | 46 let playedGame = PlayedGame(context: moc) |
44 | 47 |
45 playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! | 48 playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! |
46 playedGame.score = Int32.random(in: 0...50) | 49 playedGame.score = Int32.random(in: 0...50) |
47 playedGame.date = Date() | 50 |
51 let dayComponent = DateComponents(day: Int.random(in: -5...0)) | |
52 playedGame.date = Calendar.current.date(byAdding: dayComponent, to: Date()) | |
48 | 53 |
49 if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { | 54 if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { |
50 playedGame.correctAnswers = ["Bangladesh", "Belgium", "Burkina Faso", "Bermuda", "Jamaica"] | 55 playedGame.correctAnswers = ["Bangladesh", "Belgium", "Burkina Faso", "Bermuda", "Jamaica"] |
51 playedGame.wrongAnswers = ["Belarus", "Russia"] | 56 playedGame.wrongAnswers = ["Belarus", "Russia"] |
52 } else { | 57 } else { |