新应用程序版本的iOS应用程序数据文件应如何从NSDocumentDirectory移动到NSLibraryDirectory?

新应用程序版本的iOS应用程序数据文件应如何从NSDocumentDirectory移动到NSLibraryDirectory?,ios,nsfilemanager,nsdocumentdirectory,app-update,nslibrarydirectory,Ios,Nsfilemanager,Nsdocumentdirectory,App Update,Nslibrarydirectory,在当前版本的应用程序中,我在NSDocumentDirectory中存储了两个重要的应用程序文件(my Core Data.sqlite文件和my SettingsFile.plist)。在下一个版本中,我想将这些文件移动到NSLibrary目录。两者都不需要用户直接访问以进行编辑,根据我的研究,这似乎是每个文件的最佳选择 我关心的是如何将所有当前应用程序用户的文件从NSDocumentDirectory移动到NSLibraryDirectory。我要非常小心,不要丢失任何用户的数据。我不知道从

在当前版本的应用程序中,我在NSDocumentDirectory中存储了两个重要的应用程序文件(my Core Data.sqlite文件和my SettingsFile.plist)。在下一个版本中,我想将这些文件移动到NSLibrary目录。两者都不需要用户直接访问以进行编辑,根据我的研究,这似乎是每个文件的最佳选择


我关心的是如何将所有当前应用程序用户的文件从NSDocumentDirectory移动到NSLibraryDirectory。我要非常小心,不要丢失任何用户的数据。我不知道从哪里或如何开始以非常安全的方式移动这些文件。我希望有人能给我指出正确的方向。

您可以使用NSFileManager检查文件是否已经存在于库文件夹中,如果不存在,请移动它们。如果移动成功,则删除旧文件并返回新路径,否则返回旧路径。见下文:

...
NSString *mySQLfilePath = [self getFilePathWithName:@"database.sqlite"];
...

- (NSString *)getFilePathWithName:(NSString *)filename
{
    NSString *libPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSString *fileLibPath = [libPath stringByAppendingPathComponent:filename];
    NSString *fileDocPath = [docPath stringByAppendingPathComponent:filename];

    if ([[NSFileManager defaultManager] fileExistsAtPath:fileLibPath]) {
        // file already exists in Library folder
        return fileLibPath;

    } else if ([[NSFileManager defaultManager] fileExistsAtPath:fileDocPath]) {
        // file exists in Documents folder, so move it to Library
        NSError *error = nil;
        BOOL moved = [[NSFileManager defaultManager] moveItemAtPath:fileDocPath toPath:fileLibPath error:&error];
        NSLog(@"move error: %@", error);

        // if file moved, you can delete old Doc file if you want
        if (moved) [self deleteFileAtPath:fileDocPath];

        // if file moved successfully, return the Library file path, else, return the Documents file path
        return moved ? fileLibPath : fileDocPath;
    }

    // file doesn't exist
    return nil;
}

- (void)deleteFileAtPath:(NSString *)filePath
{
    if ([[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        NSLog(@"delete error: %@", error);

    } else {
        NSLog(@"can not delete file: %@", filePath);
    }
}
如果你有问题,请随时提问。仅供参观者参考:


您可以使用NSFileManager检查文件是否已存在于库文件夹中,如果不存在,请移动它们。如果移动成功,则删除旧文件并返回新路径,否则返回旧路径。见下文:

...
NSString *mySQLfilePath = [self getFilePathWithName:@"database.sqlite"];
...

- (NSString *)getFilePathWithName:(NSString *)filename
{
    NSString *libPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSString *fileLibPath = [libPath stringByAppendingPathComponent:filename];
    NSString *fileDocPath = [docPath stringByAppendingPathComponent:filename];

    if ([[NSFileManager defaultManager] fileExistsAtPath:fileLibPath]) {
        // file already exists in Library folder
        return fileLibPath;

    } else if ([[NSFileManager defaultManager] fileExistsAtPath:fileDocPath]) {
        // file exists in Documents folder, so move it to Library
        NSError *error = nil;
        BOOL moved = [[NSFileManager defaultManager] moveItemAtPath:fileDocPath toPath:fileLibPath error:&error];
        NSLog(@"move error: %@", error);

        // if file moved, you can delete old Doc file if you want
        if (moved) [self deleteFileAtPath:fileDocPath];

        // if file moved successfully, return the Library file path, else, return the Documents file path
        return moved ? fileLibPath : fileDocPath;
    }

    // file doesn't exist
    return nil;
}

- (void)deleteFileAtPath:(NSString *)filePath
{
    if ([[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        NSLog(@"delete error: %@", error);

    } else {
        NSLog(@"can not delete file: %@", filePath);
    }
}
如果你有问题,请随时提问。仅供参观者参考:


您不能简单地测试文件是否在库目录中,如果是,从那里使用它,如果它在文档目录中,请将它移到另一个目录中,然后开始使用它吗?我不明白其中的复杂性。难道你不能简单地测试一下文件是否在库目录中,如果是,从那里使用它,如果它在文档目录中,把它移到另一个目录中,然后开始使用它吗?我不明白其中的复杂性。
[NSHomeDirectory()stringByAppendingPathComponent:@“Library”]?我不这么认为……NSHomeDirectory():
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355
NSLibrary目录:
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355/Library
NSDocuments目录:
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355/文档
您应该使用
NSSearchPathForDirectories
。这是一样的。。为什么要使用数组,然后选择唯一的对象[0],如果您可以只使用上述对象的话?因为它告诉您在您提供的链接中确实要使用它。
[NSHomeDirectory()stringByAppendingPathComponent:@“Library”]?我不这么认为……NSHomeDirectory():
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355
NSLibrary目录:
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355/Library
NSDocuments目录:
/Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355/文档
您应该使用
NSSearchPathForDirectories
。这是一样的。。为什么要使用数组,然后选择唯一的对象[0],如果您可以只使用上述对象的话?因为它告诉您在提供的链接中确实使用了该对象。