从icloud备份中限制sqlite-wal和sqlite-shm

kum*_*mar 7 sqlite core-data ios icloud

我是第一次使用coredata,我必须从文档目录中的iCloud备份限制sqlite db文件,我使用下面的代码完成了它

-(id)init
{
    if((self = [super init]))
    {
        NSURL* documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
        NSURL* giveForwardSqliteURL = [documentsDirectoryURL URLByAppendingPathComponent:@"InfoCollection.sqlite"];
        NSError* error;

        m_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
        m_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:m_managedObjectModel];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


        if ([m_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:giveForwardSqliteURL options:options error:&error])
        {
            m_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
            [m_managedObjectContext setPersistentStoreCoordinator:m_persistentStoreCoordinator];

            [self addSkipBackupAttributeToItemAtPath:giveForwardSqliteURL];
        }
        else
        {
            NSLog(@"Failed to create or open database: %@", [error userInfo]);
            return nil;
        }
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)

//阻止iCloud备份文档目录文件夹

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSURL *) URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                              forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}
Run Code Online (Sandbox Code Playgroud)

现在我不明白的是,我们还需要从icloud备份中限制sqlite-wal和sqlite-shm文件,如果是,那么如何从icloud备份限制sqlite-wal和sqlite-shm文件

我想要一个解决方案,而无需从文档目录文件夹中更改sqlite db位置...我们怎么做

如果上述代码中有任何错误,请更正

提前致谢

Jod*_*ins 1

我想要一个解决方案,而不需要更改文档目录文件夹中的 sqlite 数据库位置...我们该怎么做

首先,请确保您已阅读本文以确定放置文件的正确目录。

其次,不要太拘泥于将它们直接放入文档目录中。一般来说,将每个文件直接放入 Documents 目录并不是一个好主意。相反,您应该使用目录层次结构对数据进行逻辑分区。

接下来,请注意,如果您从备份中排除某个目录,则该目录中的所有文件也将被排除。

因此,如果您愿意灵活一点并遵循上面的建议,您可以简单地使用文档目录的子目录作为核心数据数据库。

NSURL* giveForwardDirURL = [documentsDirectoryURL URLByAppendingPathComponent:@"InfoCollection.dir"];
[[NSFileManager defaultManager] createDirectoryAtURL:giveForwardDirURL
                         withIntermediateDirectories:YES
                                          attributes:nil
                                               error:NULL];
[self addSkipBackupAttributeToItemAtPath:giveForwardDirURL];

NSURL* giveForwardSqliteURL = [giveForwardDirURL URLByAppendingPathComponent:@"InfoCollection.sqlite"];
Run Code Online (Sandbox Code Playgroud)

但是,如果您坚持将文件驻留在该目录中,则必须在初始化时搜索它们,然后您必须在应用程序运行时监视该目录,以便在相关文件显示时设置标志向上。

该线程包含与以下内容相关的信息:iPhone's /Documents 目录更改的通知