How to VACUUM a Core Data SQLite db? How to VACUUM a Core Data SQLite db? sqlite sqlite

How to VACUUM a Core Data SQLite db?


The supported way to do this, on OS X 10.6 / iOS 3.0 and later, is to set the NSSQLiteManualVacuumOption in the options when you are adding the store to the persistent store coordinator.


Yes, vacuum is a recognized SQL statement in SQLite. It can be used as a normal query, or so it says.

Beware, though, as it can lead to excessive file system reads and writes—the bottleneck of practically any system—not to mention server file system fragmentation on Windows servers.


Here's how you can vacuum your CoreData container

Example:

let container = NSPersistentContainer(name: "someName")// Enable history tracking and remote notificationsguard let description = container.persistentStoreDescriptions.first else {    fatalError("###\(#function): Failed to retrieve a persistent store description.")}description.setOption(true as NSNumber, forKey: NSSQLiteManualVacuumOption)//... The rest of your setup

By doing this, the CoreData container will perform the vacuum command and thus resize itself accordingly.