NSInternalInconsistencyException 导致崩溃,即使它被包装在 do-try-catch 中


// context is an instance of NSManagedObjectContext
context.performAndWait {
    do {
        guard context.hasChanges else {
            return
        }
        try context.save()
    } catch {
        print("Error...")
    }
}

有人理解为什么包含try context.save()的行会导致以下崩溃吗?

致命异常:NSInternalConferenceException此NSPersistentStoreCoordinator没有永久存储(磁盘已满(。它无法执行保存操作

这是堆栈跟踪的一部分:

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x19d4c99d8 __exceptionPreprocess
1  libobjc.A.dylib                0x1b184fb54 objc_exception_throw
2  CoreData                       0x1a33b42d8 -[NSPersistentStoreCoordinator _coordinator_you_never_successfully_opened_the_database_io_error:]
3  CoreData                       0x1a33b4370 -[NSPersistentStoreCoordinator _introspectLastErrorAndThrow]
4  CoreData                       0x1a33b49c8 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke.797
5  CoreData                       0x1a324b408 -[NSPersistentStoreCoordinator _routeHeavyweightBlock:]
6  CoreData                       0x1a324c2d0 -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
7  CoreData                       0x1a324c2fc -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
8  CoreData                       0x1a324d270 -[NSManagedObjectContext save:]

抛出方法被封装在do try catch中,这应该可以防止应用程序崩溃,但Firebase(Crashlytics(将其报告为崩溃。

NSExceptions不由do/catch子句处理;Error是。

不过,您可以使用Objective-C子句捕获NSException,但通常它们对于从实际的潜在问题中恢复是无用的;相反,他们只是压制它。

如果您有这样的异常,那么您在代码的其他地方出错了。要真正阻止异常的发生,您需要修复该错误。在您的情况下,可以在堆栈跟踪中观察到有问题的问题:

_coordinator_you_never_successfully_opened_the_database_io_error

这暗示你的应用程序可能没有正确设置你的持久存储。也许,在应用程序的初始加载过程中,NSPersistentStoreCoordinatoraddPersistentStore方法中发生了I/O错误,但您没有处理?

最新更新