2
votes

My app implements a web cache using Core Data with an SQLite store (a bit like NSURLCache but with more control over what is cached and when it can be purged from the cache). I can purge old data from the cache when it gets too big, but of course this does not actually reduce the amount of storage space used in the file system.

I know that NSPersistentStoreCoordinator can be passed the NSSQLiteManualVacuumOption option to do an SQLite vacuum on opening the store, and I thought this might be a good way to reduce the file size (even if I have to temporarily close and re-open the store).

However, I am managing the Core Data stack through an NSPersistentContainer. Is there any way to do an Apple-approved SQLite vacuum with an NSPersistentContainer?

Or do I have to establish the Core Data stack manually to do this?

Or otherwise how else should I reclaim SQLite storage space when using an NSPersistentConainer?

CORRECTION:

I was partly wrong about the space not being reclaimed automatically. Even without setting NSSQLiteManualVacuumOption, the disk space is being reclaimed automatically - sometimes. I'm not sure how this is working. It doesn't happen immediately and it doesn't happen every time I save the context, but when it does happen, it is very quickly after deleting managed objects from the context and saving the context.

If I do a series of delete/save commands, the storage space is not freed in between them. But if I check again shortly afterwards, the space has been reclaimed (sometimes).

Perhaps Core Data is using the 'auto_vacuum' SQLite pragma by default? Although if I log the sqlite pragmas, it just prints an empty dictionary.

1

1 Answers

2
votes

I think it should work like this; I've never done it myself:

Create an NSPersistentStoreDescription and set the options to include the NSSQLiteManualVacuumOption. Assign this description to the the NSPersistentContainer's persistentStoreDescriptions property before loading the store.