Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将CoreStore SQLiteStore移动到其他位置_Ios_Swift_Corestore - Fatal编程技术网

Ios 将CoreStore SQLiteStore移动到其他位置

Ios 将CoreStore SQLiteStore移动到其他位置,ios,swift,corestore,Ios,Swift,Corestore,在我使用CoreStore 5.3的应用程序中,我试图将本地sqlite存储移动到另一个位置,以便能够从应用程序扩展访问数据库。使用FileManager.default.moveItem(at:oldURL,to:newURL)移动当前3个文件(MyApp.sqlite、MyApp.sqlite-wal和MyApp.sqlite-shm)时效果很好,但我不认为这样做是个好主意 if FileManager.default.fileExists(atPath: oldFileURL.path)

在我使用CoreStore 5.3的应用程序中,我试图将本地sqlite存储移动到另一个位置,以便能够从应用程序扩展访问数据库。使用FileManager.default.moveItem(at:oldURL,to:newURL)移动当前3个文件(MyApp.sqlite、MyApp.sqlite-wal和MyApp.sqlite-shm)时效果很好,但我不认为这样做是个好主意

if FileManager.default.fileExists(atPath: oldFileURL.path) {
    do {
        try FileManager.default.moveItem(at: oldFileURL, to: newFileURL)
        try FileManager.default.moveItem(at: oldJournalingURL, to: journalingURL)
        try FileManager.default.moveItem(at: oldSharedMemoryURL, to: sharedMemoryURL)
    } catch let error {
        // error handling ...
    }
}
我知道NSPersistentStoreCoordinator上有用于 migratePersistentStore()

因此,您通常会这样做,例如:

if let persistentStore = persistentStoreCoordinator.persistentStores.first {
    do {
        try persistentStoreCoordinator.migratePersistentStore(persistentStore, to: newFileURL, options: nil, withType: NSSQLiteStoreType)
    } catch {
        print("error")
    }
}
。。。因为这也会移动所有关联的文件

然而,显然这种方法并没有在CoreStore中公开。 由于persistentStoreCoordinator是DataStack的一个内部属性,在DataStack已经设置好之后,访问它的唯一方法是通过unsefectContext(),但是这会造成更多的麻烦,因为上下文然后尝试do save(),这在移动持久存储时无法工作


使用CoreStore还有其他方法吗?

您是对的,CoreStore目前没有其他方法可以做到这一点。部分原因是,确实没有办法安全地执行此操作。在移动这些文件之前,CoreStore无法知道上面提到的任何文件都没有在其他地方使用

对于SQLite存储,移动您提到的3个文件可能是最简单的实现。如果对二进制属性使用“外部存储”,还需要移动名为
\u SUPPORT
的文件夹。因此,如果您的sqlite文件名为
mydata.sqlite
,则此文件夹将命名为
.mydata\u SUPPORT


我的建议是为您的商店创建一个专用文件夹,并只将SQLite相关文件放在那里。因此,下次您要移动目录时,只需移动文件夹本身。

是的,现在最有意义。谢谢你的建议。