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

Does anyone understand why the line containing try context.save() would appear to cause the following crash?

Fatal Exception: NSInternalInconsistencyException This NSPersistentStoreCoordinator has no persistent stores (disk full). It cannot perform a save operation

This is part of the stack trace:

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:]

The throwing method is wrapped in a do-try-catch, which should prevent the application from crashing, yet this is being reported as a crash by Firebase (Crashlytics).

Francesco Puglisi
  • 2,023
  • 2
  • 18
  • 24
  • 2
    Because it's not an `(NS)Error`, it's an `NSInternalInconsistencyException`, a `NSException`. You can't catch them. With your `do/try/catch`, you'll catch what's thrown by the tried method which are `(NS)Error` Here `save`. But that's one is crashing, not throwing an error. That's the quick explaination. – Larme Apr 29 '21 at 13:29
  • Now that you see the diff: https://stackoverflow.com/questions/32758811/catching-nsexception-in-swift etc. For your issue, it means that you might have a bigger problem to fix, than a simple try/catch. – Larme Apr 29 '21 at 13:32

1 Answers1

1

NSExceptions are not handled by do/catch clauses; Errors are.

You can catch NSExceptions with Objective-C clauses, though, but typically they're useless for recovering from the actual underlying issue; rather, they just suppress it.

If you have an exception like this, then you made an error somewhere else in your code. To really stop Exceptions from occurring, you need to fix that error. In your case, the issue in question can be observed in the stack trace:

_coordinator_you_never_successfully_opened_the_database_io_error

which hints that your app probably didn't set up your persistent store properly. Perhaps, an I/O error occurred in your NSPersistentStoreCoordinator's addPersistentStore method during the app's initial loading, which you didn't handle?

Vym
  • 301
  • 1
  • 6