我是第一次使用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位置…我们怎么做
如果上述代码中有任何错误,请更正
提前致谢
解决方法
And i want a solution without changing the sqlite db location from
documents directory folder… how can we do it
首先,请确保您已阅读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.