备份和恢复核心数据的SQLite(核心数据)
问题描述:
我已经找遍在SO谷歌等我似乎无法找到一个解决方案或适合手动备份和恢复我的核心数据sqlite的文件教程。目标是让用户从文档目录中手动恢复Core Data SQlite文件。备份和恢复核心数据的SQLite(核心数据)
This seems promising,但仅仅是复杂的我的需要或相关章节extrapulate我的问题。我只想复制希望iCloud所做的备份文档目录,而无需用户每次需要恢复数据时都必须将设备恢复备份。我希望iOS 8中的cloudkit能够让这个更简单,但目前为止没有点击。
总结:我如何获得这些文件的目录达到iCloud中的内容,然后再检索。我不是在同步之后,只是远程存储和检索文档目录。香港专业教育学院读了一些人曾与Dropbox的成功对于这一点,持开放的态度,以
UPDATE
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite"];
NSString *filePath2 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite-shm"];
NSString *filePath3 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"iCertifi.sqlite-wal"];
//Path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *archivePath = [documentsDirectory stringByAppendingString:@"/files.zip"];
//Zip up documents directory
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:filePath newname:@"backUp.sqlite"];
[archiver addFileToZip:filePath2 newname:@"backUp.sqlite-shm"];
[archiver addFileToZip:filePath3 newname:@"backUp.sqlite-wal"];
答
我所做的iCloud文档,CloudKit和Dropbox。
Dropbox是最简单的开始。
我拉上我的数据库:
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:withPath newname:filename]; //@"x.sqlite"
[archiver addFileToZip:withPath newname:filename]; //@"x.sqlite-shm"
[archiver addFileToZip:withPath newname:filename]; //@"x.sqlite-wal"
然后把它送上去给收存箱
NSString *dropBoxPath = [NSString stringWithFormat:@"/%@/%@",kBackupFolder,fileToUpload];
DBPath *path = [[DBPath alloc] initWithString:dropBoxPath];
NSString *sourcePath = xxxxx;
if (!info) {
DBFile *backupFile = [[DBFilesystem sharedFilesystem] createFile:path error:&createFileError];
DBError *writeFileError;
[showFile writeContentsOfFile:sourcePath shouldSteal:NO error:&writeFileError];
你可以等待它上传:
while (showFile.status.state == DBFileStateUploading || showFile.status.error) {
[NSThread sleepForTimeInterval:1.0f];
fileprogress = showFile.status.progress;
链接:
感谢您的回复,我已经试过你的建议(代码更新),但是拉链不会打开的时候在桌面上,也sqlite的文件不是文件目录只是新的空文件的副本。我感兴趣的是你如何从Dropbox中取回他们并删除你现有的数据库?这可以在iTunes共享中手动完成,但不是非常优雅 – JSA986 2014-10-31 11:42:34
如果文件为空,则没有正确的文件名(您需要完整路径),您可能需要:NSAssert([NSFileManager defaultManager] fileExistAtPath:xxxx ],@“文件必须存在”); – 2014-10-31 16:16:45
嗨,作为这个老问题,我只是想知道你是如何解决你的问题的,我现在做的完全一样,但是,我的问题是压缩文件需要打开,当我恢复它时,甚至在桌面上;尽管我使用了上述答案中提到的相同的压缩库。 – user3298158 2017-02-20 19:46:53