xcode 如何使用 addSkipBackupAttributeToItemAtURL API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13498752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use addSkipBackupAttributeToItemAtURL API?
提问by Momi
my application rejected due to this issue :
由于这个问题,我的申请被拒绝:
My application is a dictionary which uses SQL DBs , with bookmarking and etc ...
我的应用程序是一个使用 SQL DB 的字典,带有书签等......
so I copied this code from Apple docs in appDelegate.m :
所以我从 appDelegate.m 中的 Apple 文档中复制了这段代码:
- (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;
}
but used like this :
但像这样使用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:@"<myApplication>/Library/Caches"]];
}
but crashes with this reason :
但由于这个原因而崩溃:
Assertion failed: ([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]), function -[AppDelegate addSkipBackupAttributeToItemAtURL:], file /iData/Development 2011/My iPhone Project/APPNAME/APPNAME/AppDelegate.m, line 27.
断言失败:([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]),函数 -[AppDelegate addSkipBackupAttributeToItemAtURL:],文件 /iData/Development 2011/My iPhone Project/APPNAME/APPNAME/AppDelegate.m,第 27 行。
According to that picture is this my only problem for rejection ? because this is my first time I work with data base .
根据那张照片,这是我拒绝的唯一问题吗?因为这是我第一次使用数据库。
Thanks . ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
谢谢 。~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDITED :
编辑:
- (NSString *) getDBPath
{
NSInteger kValue = [[[NSUserDefaults standardUserDefaults] stringForKey:@"Bv"] intValue];
NSString *documentsDir = [[NSString alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
documentsDir = [paths objectAtIndex:0];
switch (kValue) {
case 0:
documentsDir = [NSString stringWithFormat:@"%@/eng-per.sqlite", documentsDir];
break;
case 1:
documentsDir = [NSString stringWithFormat:@"%@/eng-per.sqlite", documentsDir];
break;
case 2:
documentsDir = [NSString stringWithFormat:@"%@/per-eng.sqlite", documentsDir];
break;
}
return documentsDir
}
}
and then changed to this in app delegate.m :
然后在 app delegate.m 中更改为:
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:dbClass.getDBPath]];
now app lunches fine without crash
现在应用程序午餐很好,没有崩溃
采纳答案by Michael Dautermann
Your app is "crashing" because you're hitting that assert on line:
您的应用程序“崩溃”,因为您在线上点击了该断言:
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
And I suspect your problem is actually in how you are setting up the URL:
我怀疑您的问题实际上出在您设置 URL 的方式上:
[NSURL URLWithString:@"<myApplication>/Library/Caches"];
How are you getting the path to "<myApplication>"?
你是如何获得“ <myApplication>”的路径的?
You need to get your application's true Cache's folder, which you can do via:
您需要获取应用程序的真正缓存文件夹,您可以通过以下方式进行:
[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];(for the location passed back via file URL's)
[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];(对于通过文件 URL 传回的位置)
or
或者
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);(for the path in the form of a NSString)
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);(对于 NSString 形式的路径)
So ultimately, what you need to do is notassert if the key has already been set in defaults and you need to properly set the file URL for the file you're saving.
因此,最终,您需要做的不是断言密钥是否已设置为默认值,并且您需要为要保存的文件正确设置文件 URL。
Create your URL (a file URL, to be precise) using something like:
使用以下内容创建您的 URL(准确地说是文件 URL):
NSArray * arrayOfURLs = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
// make certain there's at least one file url returned by the above call
if([arrayOfURLs count] > 0)
{
// and this would just be the URL to the cache directory...
NSURL * cacheDirectoryPath = [arrayOfURLs objectAtIndex: 0];
// ... to create a file url to your actual dictionary, you'd need to add a
// filename or path component.
// Assuming you save this as a property within your object
self.cacheFileURL = [cacheDirectoryPath URLByAppendingPathComponent: @"myDatabase.db"];
}

