q0r*_*0re 2 iphone core-data objective-c ios
在我的应用程序中,我必须存储核心数据数据库.我在xcode项目导航器中有一些包含默认数据(audiofiles,maptiles等)的组和文件夹.
我发现很多关于阻止文件备份的事情:我做了什么:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"db.sqlite"];
NSLog(@"%@", storeURL.path);
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
//NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES};
/*NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];*/
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self addSkipBackupAttributeToItemAtURL:storeURL];
return _persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
预防方法:
- (BOOL)addSkipBackupAttributeToItemAtURL:(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)
(最低目标iOS版本为7.0)
这够了吗?如何检查应用程序现在是否阻止备份核心数据数据库?
在我添加addSkipBackupAttributeToItemAtURL方法之前,我检查了应用程序存储,在文档和数据下找不到任何内容.我只在备份下找到3.1 MB - >我的iphone
- Install and launch your app
- Go to Settings > iCloud > Storage & Backup > Manage Storage
- If necessary, tap "Show all apps"
- Check your app's storage
Run Code Online (Sandbox Code Playgroud)
你做得对.继Apple Technical Q&A 1719之后:如何阻止文件备份到iCloud和iTunes?你应该标记"不要备份"属性.
对于NSURL对象,请添加该NSURLIsExcludedFromBackupKey属性以防止备份相应的文件.对于CFURLRef对象,请使用相应的kCFURLIsExcludedFromBackupKey属性.
要了解更适合存储Core Data数据库的文件夹,您可以查看iOS数据存储指南.
| 归档时间: |
|
| 查看次数: |
1364 次 |
| 最近记录: |