3 struct PersistenceController {
4 static let shared = PersistenceController()
6 static var preview: PersistenceController = {
7 let result = PersistenceController(inMemory: true)
8 let viewContext = result.container.viewContext
10 let newMap = Map(context: viewContext)
12 newMap.createdAt = Date()
13 newMap.title = "Map \(newMap.createdAt!.format())"
17 try viewContext.save()
19 let nsError = error as NSError
20 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
25 let container: NSPersistentCloudKitContainer
27 init(inMemory: Bool = false) {
28 container = NSPersistentCloudKitContainer(name: "Map")
30 container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
31 container.viewContext.automaticallyMergesChangesFromParent = true
34 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
36 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
37 if let error = error as NSError? {
38 fatalError("Unresolved error \(error), \(error.userInfo)")