如何使用addSkipBackupAttributeToItemAtURL API?

7

我的应用由于以下问题被拒绝:

http://i.imgur.com/yajQh.png

我的应用是一个使用 SQL 数据库的词典,具备书签等功能...

因此,我在 appDelegate.m 中从苹果文档中复制了以下代码:

- (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;
}

但是用法如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:@"<myApplication>/Library/Caches"]];
}

但是出现了以下原因而崩溃:

断言失败:([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]), 函数 -[AppDelegate addSkipBackupAttributeToItemAtURL:], 文件 /iData/Development 2011/My iPhone Project/APPNAME/APPNAME/AppDelegate.m, 第27行。

根据图片显示,这是我被拒绝的唯一问题吗?因为这是我第一次使用数据库。

谢谢。

编辑:

- (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

然后在app delegate.m中更改为:

[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:dbClass.getDBPath]];

现在应用程序可以正常启动而不会崩溃

1个回答

4

你的应用程序“崩溃”是因为你在该行触发了断言:

assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

我猜测您的问题实际上在于您如何设置URL:
[NSURL URLWithString:@"<myApplication>/Library/Caches"];

你是如何获取 "<myApplication>" 的路径的?
你需要获取你的应用程序真正的缓存文件夹,你可以通过以下方式实现: [NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]; (对于通过文件URL返回的位置)或者 NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); (对于以NSString形式表示的路径)。
因此,最重要的是,你不应该假定键已经在默认值中设置,并且应该正确设置要保存的文件的文件URL。
使用类似以下内容的方式创建你的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"];
}

4
你必须使用 NSURL fileURLWithPath: 而不是 NSURL URLWithString: 来从文件路径创建 NSURL - rmaddy
NSSearchPathForDirectoriesInDomains 返回的是路径字符串数组,而不是 NSURL 对象。 - rmaddy
非常感谢您,您能否看一下编辑后的问题?我做了一些更改,它似乎有效,但我仍然不确定是否备份成功了... - Mc.Lover
1
我不确定它是否备份。但是在您的“编辑”代码中,我看到所有三种情况似乎都生成了完全相同的路径字符串。 - Michael Dautermann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接