gpt4 book ai didi

cocoa - 迁移持久存储 : causes duplicate data when destination URL exists

转载 作者:行者123 更新时间:2023-12-03 16:20:08 26 4
gpt4 key购买 nike

刚刚遇到一个我想分享的场景。当使用 NSPercientStoreCoordinator migratePersistentStore: 且目标 URL 已存在时,结果数据将被合并。我的情况是,每当我的应用程序关闭时,我都会创建过时的备份,并且同一天的多次启动可能会备份相同的文件。

最佳答案

我通过使用 NSFileManager fileExistsAtPath: 和removeItemAtPath: 在调用 migratePersistentStore: 之前删除现有文件解决了该问题。这似乎已经解决了重复问题。

我找不到说明这是一项功能的文档,但也许确实如此。

已更新

我为 Jim 添加了一些示例代码。禁用journal_mode 的标志对于我的用例非常重要。 See here for more info

if ([[NSFileManager defaultManager] fileExistsAtPath: backupPath])
[[NSFileManager defaultManager] removeItemAtPath: backupPath
error: nil];

return [BRManagedObject.persistentStoreCoordinator
migratePersistentStore: store
toURL: [NSURL fileURLWithPath: backupPath]
options: @{
NSSQLiteManualVacuumOption : @(YES)
#ifndef SQLITE_USES_WRITE_AHEAD_LOG
, NSSQLitePragmasOption : @{ @"journal_mode" : @"DELETE" }
#endif
}
withType: persistentStoreType
error: nil];

关于cocoa - 迁移持久存储 : causes duplicate data when destination URL exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com