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

时间:2015-06-05 11:49:02

标签: ios sqlite core-data 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;
}

//阻止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;
}

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

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

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

提前致谢

2 个答案:

答案 0 :(得分:1)

  

我想要一个不改变sqlite db位置的解决方案   文件目录文件夹......我们怎么做呢

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

其次,不要将它们直接放入文档目录中。通常,将每个文件直接填充到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"];

但是,如果您坚持让文件驻留在目录中,则必须在初始化时搜索它们,然后您必须在应用程序运行时监视目录,这样您就可以设置标志if / when相关文件出现。

此主题包含与之相关的信息:Notification of changes to the iPhone's /Documents directory

答案 1 :(得分:-1)

您可以通过向-[NSPersistentStoreCoordinator addPersistentStore…]调用添加选项字典来强制Core Data使用回滚日记模式,阻止-shl和-wal文件,如下所示:

https://stackoverflow.com/a/24261845/453082