0

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.

enter image description here

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
        }
Zsolt
  • 3,648
  • 3
  • 32
  • 47

1 Answers1

1

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • thanks Tom. I set things up in testing with `NSSQLiteStoreType` and I am using a 10MB image.. I presume that would "force" core data to move the data to a file, but it does not seem to be the case.. – Zsolt Nov 20 '18 at 17:59
  • I don’t know what the limit is. It may change in different versions of iOS. In a quick test I was using a 250MB file. – Tom Harrington Nov 20 '18 at 19:16
  • I must be missing something. I ended up using a 300MB image and still no luck. I updated my question with the method I use for setup. – Zsolt Nov 20 '18 at 19:49
  • Nothing you can do in code affects whether external storage is used, unless you’re creating the model in code rather than with the model editor. – Tom Harrington Nov 20 '18 at 19:53
  • It's also not impossible that there's more to it than just size, because the process is entirely undocumented. – Tom Harrington Nov 20 '18 at 21:25
  • I found the threshold to be much smaller with NSPersistentDocument on macOS, about half a MB, see [Inline Link](https://stackoverflow.com/questions/45356025/using-nspersistentdocument-to-create-documents/46130680#46130680) – Dirk Nov 20 '18 at 22:36