Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created August 10, 2019 18:44
Show Gist options
  • Save PaulWoodIII/5cda7c363730fcc591fef708efb49efc to your computer and use it in GitHub Desktop.
Save PaulWoodIII/5cda7c363730fcc591fef708efb49efc to your computer and use it in GitHub Desktop.
used in Xcode test on each unit test, loads fast, is in memory and doesn't affect other test so long as you don't test concurrently
/// Just a Holder for a persistentContainer that is lazily loaded
class TestPersistentContainer {
lazy var persistentContainer: NSPersistentContainer = {
let bundle = Bundle(for: CoreDataService.self)
let modelURL = bundle.url(forResource: CoreDataModelName, withExtension: "momd")!
let mom = NSManagedObjectModel(contentsOf: modelURL)!
let container = NSPersistentContainer(name: "LateralModel",
managedObjectModel: mom)
let description = container.persistentStoreDescriptions.first!
description.url = URL(fileURLWithPath: "/dev/null")
try! container.persistentStoreCoordinator
.destroyPersistentStore(at: URL(fileURLWithPath: "/dev/null"),
ofType: NSSQLiteStoreType,
options: [:])
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment