【问题标题】:Back up and restore a Core Data SQLite (core data)备份和恢复 Core Data SQLite(核心数据)
【发布时间】:2014-10-30 03:14:45
【问题描述】:

我在 SO Google 等上到处搜索,但似乎找不到适合手动备份和恢复我的 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"]; 

【问题讨论】:

    标签: ios sqlite core-data


    【解决方案1】:

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

    链接:

    https://github.com/mattconnolly/ZipArchive

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

    【讨论】:

    • 感谢您的回复,我已经尝试了您的建议(代码已更新),但是在桌面上无法打开 zip,而且 sqlite 文件也不是文档目录的副本,只是新的空文件。我对您如何从保管箱中检索它们并删除现有数据库感兴趣?这可以在 iTunes 共享中手动完成,但不是很优雅
    • 如果文件是空的,你没有正确的文件名(你需要完整的路径),你可能想要: NSAssert([NSFileManager defaultManager] fileExistAtPath:xxxx],@"File must存在");
    • 嗨,作为这个老问题,我只是想知道你是如何解决你的问题的,我目前正在做同样的事情,但是,我的问题是当我想要打开压缩文件时恢复它甚至在桌面上;尽管我使用的是上述答案中提到的相同压缩库。
    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2018-07-14
    • 2011-01-11
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多