我第一次使用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文件

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

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

提前致谢

最佳答案



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

其次,不要被束缚于将它们直接放入文档目录中。通常,将每个文件直接填充到Documents目录不是一个好主意。相反,您应该使用目录层次结构对数据进行逻辑分区。

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

因此,如果您愿意稍微灵活一些并遵循上面的建议,则可以简单地将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"];

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

该线程包含与此有关的信息:Notification of changes to the iPhone's /Documents directory

10-07 19:56
查看更多