I'm migrating my iOS app to use NSPersistentContainer
. This class by default locates its persistent store files in the Library/Application Support
directory; previously my store files were stored in the Documents
directory.
I've added a little bit of code to move the store files if they were found at the old directory:
func moveStoreFromLegacyLocationIfNecessary(toNewLocation newLocation: URL) {
// The old store location is in the Documents directory
let legacyStoreLocation = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("books.sqlite")
// Check whether the old store exists and the new one does not
if FileManager.default.fileExists(atPath: legacyStoreLocation.path) && !FileManager.default.fileExists(atPath: newLocation.path) {
print("Store located in Documents directory; migrating to Application Support directory")
let tempStoreCoordinator = NSPersistentStoreCoordinator()
try! tempStoreCoordinator.replacePersistentStore(at: newLocation, destinationOptions: nil, withPersistentStoreFrom: legacyStoreLocation, sourceOptions: nil, ofType: NSSQLiteStoreType)
// Delete the old store
try? tempStoreCoordinator.destroyPersistentStore(at: legacyStoreLocation, ofType: NSSQLiteStoreType, options: nil)
}
}
After calling destroyPersistentStore(at: url)
, the store files are still present on disk. Will these be automatically cleaned up at some point? Or should I be deleting them? Should I also be deleting the .sqlite-shm
and .sqlite-wal
files?