Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 Core Data In Memory Store for Unit Tests

For unit tests, I've using something similar to this: https://gist.github.com/aceontech/8860058 to create an in-memory store while testing my core data models. With the introduction of iOS 10, NSPersistentStoreCoordinators are now hidden inside NSPersistentContainers. Has anyone played around with Core Data on iOS 10 and figured out how to initialize something similar? Any help would be appreciated.

Thanks!

like image 721
user754905 Avatar asked Aug 17 '16 19:08

user754905


1 Answers

You can continue using the old approach. It's not deprecated, and NSPersistentContainer isn't required.

If you want the newer approach, use the new NSPersistentStoreDescription class, which handles all the stuff that could be specified when adding a persistent store. You'd do something like

let container = NSPersistentContainer(name: "MyModel")

let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
container.persistentStoreDescriptions = [description]

container.loadPersistentStores(completionHandler: { ...
}

This new class also includes things like automatic lightweight migration options, etc.

like image 88
Tom Harrington Avatar answered Oct 31 '22 17:10

Tom Harrington