现在我的备份和恢复功能正常.问题出在.sqlite文件本身.看来.sqlite文件不完整.
我在我的应用程序中输入了大约125个条目并进行了备份.备份出现在我的Dropbox中但是当我使用.sqlite资源管理器工具查看内容时,我只看到第117个记录的记录.
我尝试更新第一个条目,然后再次观察.sqlite文件,但没有再次更改.
更奇怪的是该应用程序似乎记录了所有更改.当我添加新条目或更新现有条目并重新启动应用程序时,新添加的数据似乎仍然存在.
但是这个新添加的数据不会出现在我的.sqlite文件中.
我正在使用此代码备份:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSString *filePath = [[[appDelegate applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"MyApp.sqlite"]; if (account) { if ([filesystem isShutDown]) { filesystem = [[DBFilesystem alloc] initWithAccount:account]; [DBFilesystem setSharedFilesystem:filesystem]; } DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"Backup - %@.sqlite",[NSDate date]]]; DBFile *file = [[DBFilesystem sharedFilesystem] createFile:newPath error:nil]; [file writeContentsOfFile:filePath shouldSteal:NO error:nil]; [filesystem shutDown]; }
我还从Simulator的文件夹中复制了.sqlite文件,并尝试在.sqlite浏览器中查看它.它仍然表现出相同的行为.有必要发生这种情况的原因吗?
解决方法
基础SQLite存储文件的日记模式.这在解释中
With the WAL mode,Core Data keeps the main store file untouched and
appends transactions to a -wal file in the same location. After the
Core Data context is saved,the -wal file is not deleted,and the data
in that file is not merged to the store file either. Therefore,simply
making copies of the store file will likely cause data loss and
inconsistency.
这应该可以解释为什么单独的.sqlite文件不完整.
作为解决方案,您可以(也在技术说明中解释):
>通过设置,为SQLite存储禁用WAL模式(并使用“旧”回滚日记模式)
@{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}};
添加持久性存储时的选项,或
>使用
- (NSPersistentStore *)migratePersistentStore:(NSPersistentStore *)store toURL:(NSURL *)URL options:(NSDictionary *)options withType:(NSString *)storeType error:(NSError **)error
制作Core Data存储的备份副本的方法.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。