备份和还原Core Data SQLite(核心数据)

时间:2014-10-30 03:14:46

标签: ios sqlite core-data

我已经搜索了谷歌等所有地方,我似乎无法找到适合手动备份和恢复我的Core Data Sqlite文件的解决方案或教程。目的是让用户从文档目录手动恢复Core Data SQlite文件。

This seems promising但只是为了满足我的需求或将相关部分排除在我的问题之外。我只是希望通过备份文档目录来复制想要iCloud的功能,而无需用户每次需要将数据恢复时都必须将设备恢复到备份状态。我希望iOS 8中的cloudkit能让这个更简单,但到目前为止还没有点击过。

总结:如何将文档目录的内容提取到iCloud,然后再次检索。我不是在同步之后,只是远程存储和检索文档目录。我已经读过一些人用Dropbox取得了成功,对此开放

更新

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"]; 

1 个答案:

答案 0 :(得分:2)

我已经完成了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"

然后将其发送到dropbox

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;

链接:

https://github.com/mattconnolly/ZipArchive

https://www.dropbox.com/developers/sync