我正在迁移我的iOS应用以使用NSPersistentContainer
。默认情况下,此类将其持久性存储文件定位在Library/Application Support
目录中;以前我的商店文件存储在Documents
目录中。
如果在旧目录中找到商店文件,我添加了一些代码来移动商店文件:
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)
}
}
调用destroyPersistentStore(at: url)
后,商店文件仍然存在于磁盘上。这些会在某些时候自动清理吗?或者我应该删除它们?我是否还要删除.sqlite-shm
和.sqlite-wal
个文件?