SwiftUI UITableView NSInternalInconsistencyException in M1 simulator

Hello,
I have a weird bug that happens only on my M1 Mac mini when buiding for the simulator.
It is the typical table view NSInternalInconsistencyException error:

Code Block
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update'

The error is related to a table view instantiated and managed by SwiftUI so I have no control over datasources.
The corresponding View has a list fed a fetched result, with this property:

Code Block
var recipes: FetchedResults<Recipe> { fetchRequest.wrappedValue }


The crash happens the I add an object to the NSManagedObjectContext that stores the app contents. It happens when performing a very simple and fundamental flow of my app (a recipes app: the action is "add a recipe", imagine that).
I found the crash yesterday after moving the project to my new M1 Mac.
I verified that there is no crash or warning on the device iPhone XR) or on my older 2018 MacBook Pro.

Anyone with a suggestion, or similar bugs?

Thanks,
Davide





Answered by nuutsss in 683429022

The same error unluckily started happening also on devices.
Fortunately I found a workaround, at the end of this thread: https://developer.apple.com/forums/thread/129956?answerId=409430022#409430022.

I wapped the model-updating code into

 DispatchQueue.main.async { }

It must be a swift UI bug, but pushing these operations to the main thread seems to work. Davide

To expand on this, I have another bug, linked again to core data and swift ui.
The bug appears only in the M1 simulator.

I have an edit screen where I can add or remove "IngredientLine" items to a "Recipe" item. I have implemented swipe to delete.
When you delete an item, the table gets messed up, cells are somehow drawn (I see the correct number of cells) but they are empty. If I close this view and open it again, I see that the delete operation was successful.
Similarly, if I add an IngredientLine, via a presented sheet, when the sheet is dismissed the view load without any content.

I am quite sure I might be doing something in a way that is not the most canonical; but the bugs appear only on the M1 simulator.

Anyone else with something similar happening?

Cheers,
Davide

Accepted Answer

The same error unluckily started happening also on devices.
Fortunately I found a workaround, at the end of this thread: https://developer.apple.com/forums/thread/129956?answerId=409430022#409430022.

I wapped the model-updating code into

 DispatchQueue.main.async { }

It must be a swift UI bug, but pushing these operations to the main thread seems to work. Davide

I'm also getting this. Adding delays here and there moves the issue around. In some cases I can get it to happen 100% reliably. Then if I add a delay or move code around, it moves to another screen that uses the same query and view as a subview. This is a showstopper for me, and might require manually implementing the FRC the old way :(

The 'async' in the accepted answer did it for me. I changed from context.performAndWait({ delete action }) to await context.perform({ delete action }). and this fixed it for me.

SwiftUI UITableView NSInternalInconsistencyException in M1 simulator
 
 
Q