Core Data and FetchedResultsController bugged behaviour after application crash (or forced exit)

I am experiencing a very weird issue with Core Data. I created a simple POC application to check this better. I am using a simple FetchedResultsController as Data Source for the View Controller to show the Data. I am using a background thread (performBackgroundTask) to add an entity, when a user inputs it. Also am using automaticallyMergesChangesFromParent (or transaction history) to merge the data added from the background context to the viewContext - the wrong behaviour will be the same for any of the two options used. The application runs fine, whenever I add an entity, I see in the SQL log a notification, and the query which is triggered automatically for the FetchedResultsController, and UI is updated with the new record. Now if I crash the application exactly while a record is being added to the database - it is hard to pinpoint the moment exactly - after this point on, I restart the application, and whenever I add one more entity, the application also works fine except that the fetch query executed by the FetchedResultsController is repeated several times (had use case where I had it repeat for several seconds). I get no errors from the PersistentController, or anything that the SQL Database is corrupted; data base browsers also do not show anything suspicious, except the gap in the record primary key (index) of the missing record which was attempted to be added when the crash occurred - which is missing in the number continuity. If I reinstall the application / delete the SQL store the app works fine.

I also created a git repo for this POC - ready to download and try out:

https://github.com/warlockba/CoreDataPOC.git

Is there some way in Core Data to execute some kind of "repair" or "integrity check"? Because I have no further idea how to fix / recognise / detect the issue - since nothing is throwing any errors, except this huge amount of fetches, after the issue happens.

Thanks, Andrei

this code is losing the managed object context:

    func performInBackground(operation: BackgroundOperation) {
        container.performBackgroundTask { context in
            self.backgroundQueue.async {
                operation.execute(context: context)
            }
        }
    }

you don't need backgroundQueue. it should be this instead…

    func performInBackground(operation: BackgroundOperation) {
        container.performBackgroundTask { context in
            operation.execute(context: context)
        }
    }

Why do you say it is loosing the context? The idea of the background queue is that multiple background jobs should be executed in a FIFO order (later on in the application). It has nothing to do with background tasks of the Core Data. I just want an update then delete to be executed exactly in the queued order, otherwise there is a risk that i execute an update on an already deleted object. And how would that explain the misbehavior of the store after a crashed insertion?

Core Data and FetchedResultsController bugged behaviour after application crash (or forced exit)
 
 
Q