4

我正在迁移我的 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文件吗?

4

0 回答 0