Wednesday, January 27, 2010

Why Does UITableView Update Sometimes Crash

In some rare cases application can crash, when you do UITableView update (Out of Bounds error). This is more easy in multi-threading applications using NSMutableArray, NSMutableDictionary etc. as data source, where data source is written in non-Main thread(s) and read in Main thread for screen UI update.

Add there some asynchronous calls with automatic data change notifications just to complicate things.

Things which might help:
  • Random delay(s) in random places, maybe before table update starts
  • @synchronized directive to control multi-thread access
  • NSLock to control read/write access
  • Remember index of last safe data item count
Multi-threading and asynchronous operations might be needed to keep UI responsive, mutexes could help keep things under control, automated notifications might offer better architecture.

FIRST update your data, THEN update your table. If you cannot guarantee that, be prepared for endless debugging of just one more crash.

No comments:

Post a Comment