【问题标题】:crash on calling addPersistentStoreWithType with an invalid URL - File at path does not appear to be a SQLite database使用无效 URL 调用 addPersistentStoreWithType 时崩溃 - 路径中的文件似乎不是 SQLite 数据库
【发布时间】:2013-09-30 16:24:51
【问题描述】:

当一个损坏/非sqlite被传递给时,我需要处理核心数据错误

- (NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:(NSString *)configuration URL:(NSURL *)storeURL options:(NSDictionary *)options error:(NSError **)error

它会导致崩溃。崩溃清楚地描述了错误

未解决的错误 Error Domain=NSCocoaErrorDomain Code=259“操作无法完成。(Cocoa 错误 259。)”UserInfo=0xb925300 {NSUnderlyingException=路径上的文件似乎不是 SQLite 数据库:

假设在这种情况下将返回 nil 值,我以这种方式处理它

if (![storeCoordeinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
};

但它仍然崩溃。在这种情况下处理此错误的正确方法是什么。 ?

【问题讨论】:

    标签: ios objective-c sqlite core-data


    【解决方案1】:

    使用 try-catch 块。 https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html在页面底部

    BOOL isValidDatabaseFile = YES;
    @try
    {
    if (![storeCoordeinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) 
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    };
    }
    @catch(NSException *ex)
    {
        isValidDatabaseFile = NO;
    }
    @finally
    {
    }
    

    然后检查isValidDatabaseFile

    【讨论】:

    • 同意。在这种情况下,该方法应该只返回一个错误,但事实并非如此。捕获异常是唯一合理的选择。
    • 有什么方法可以检查接收到的文件(保存到文件中的 NSData 响应)是否与 SQLite 数据库兼容。所以处理起来会容易很多。在上述情况下,即使捕获到异常,我也无法正确处理流程
    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多