Reputation: 3760
I am trying to test core data's External Storage
. Is there a way to force core data to write the data to a file? Adding a large data/image does not seem to work.
https://stackoverflow.com/a/7926505/429763
func setupOnDiskTestStore() {
let mom = NSManagedObjectModel.mergedModel(from: [Bundle.main, Bundle(for: type(of: self))])
psc = NSPersistentStoreCoordinator(managedObjectModel: mom!)
let store = try! psc.addPersistentStore(ofType: NSSQLiteStoreType,
configurationName: nil,
at: storeURL(),
options: nil)
expect(store).notTo(beNil())
moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
moc.persistentStoreCoordinator = psc
}
Upvotes: 0
Views: 81
Reputation: 70966
There's no way to force it to use external storage. The checkbox says it's allowed, but there's no way to make it required. As noted in the page you link to, it's related to the size of the data, so larger data blobs will be stored externally.
For testing only-- you can inspect the contents of the external storage directory to see what's there. This is completely undocumented, so you can't rely on it in an app, but it might be useful for testing. The data goes in a hidden directory in the same directory as your persistent store file. For example if your persistent store is named MyData.sqlite
and it's located in the application support directory (which is where NSPersistentContainer
puts it, unless you tell it to use a different location), then the external storage (if any) will be in Application Support/.MyData_SUPPORT/_EXTERNAL_DATA/
. There will be one file per externally stored data object.
You can't match the files to managed objects by name, because the file names are UUIDs and the UUIDs aren't available in code. But if you were to create a single new managed object with external storage enabled, and you then found that there was a single new file in that directory, you'd know that the new file corresponds to the new object.
Upvotes: 1