Skip to content

Instantly share code, notes, and snippets.

@novinfard
Created February 27, 2019 16:07
Show Gist options
  • Save novinfard/dd13ef2440fb4cda877bbb211ae00ec3 to your computer and use it in GitHub Desktop.
Save novinfard/dd13ef2440fb4cda877bbb211ae00ec3 to your computer and use it in GitHub Desktop.
[differences between loading persistent container within the app or unit test
// Inside the app (SQLite)
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoreDataUnitTesting")
container.loadPersistentStores { (description, error) in
if let error = error {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
return container
}()
// Inside the test (in-memory)
lazy var mockPersistantContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoreDataUnitTesting", managedObjectModel: self.managedObjectModel)
let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
description.shouldAddStoreAsynchronously = false
container.persistentStoreDescriptions = [description]
container.loadPersistentStores { (description, error) in
precondition(description.type == NSInMemoryStoreType)
if let error = error {
fatalError("In memory coordinator creation failed \(error)")
}
}
return container
}()
var managedObjectModel: NSManagedObjectModel = {
let managedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle.main])!
return managedObjectModel
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment