Exception .... Attempted to access the table view's visibleCells while they were in the process of being updated, which is not allowed.

On iOS13 snd iPadOS on a call to visibleCells on a UITableView ( on code which works fine on iOS9 to iOS12 ) I get an exception with the message


Attempted to access the table view's visibleCells while they were in the process of being updated, which is not allowed.


Searching on the web gives me no hits for this exact message, so I suspect the message itself is new or changed.



Now the general gist of the message is fairly clear, but the specifics and but how one is expected to avoid this in a multithreaded application are far from clear.


For example ...what sort of changes qualify,


For example ... the user may rotate the device, a redisplay may be caused by incoming data. Are these now to be expected to crash your application?


Maybe there is a new threading model / usage requirement that I don't know about, yet.


Synchronising on the UITableView itself doesn't help.


What am I missing



And Yes, I can avoid the problem by catching the error and backing off until I succeed. But the hit on performance is eyewatering. And it's horrible.

Accepted Reply

UITableView beginUpdates / end updates is not documented as a locking mechanism but one of grouping updates for smooth animation.


Despite that, it appears that it does some locking.


Although my code is making no changes, accessing the value of visibleCells, wrapping the code in a beginUpdates / endUpdates pair allows it to complete without the "Attempted to access the table view's visibleCells while they were in the process of being updated" error.

Replies

UITableView beginUpdates / end updates is not documented as a locking mechanism but one of grouping updates for smooth animation.


Despite that, it appears that it does some locking.


Although my code is making no changes, accessing the value of visibleCells, wrapping the code in a beginUpdates / endUpdates pair allows it to complete without the "Attempted to access the table view's visibleCells while they were in the process of being updated" error.

I have the same Exception when I call becomeFirstResponder on UITextField which is in the cell view. The code is working on Simulator but not on physical device.