Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView stuck in reloadData

I'm having problems with [UICollectionView reloadData], which seems to get stuck for quite a while before it calls numberOfSectionsInCollectionView on its delegate that I have specified. This is what happens:

The app starts with the UICollectionView immediately visible and numberOfSectionsInCollectionView is called on the delegate, which returns the number of items in an empty array (i.e. 0) and immediately starts to download the data for the array from a web service. Once the download is complete, the data is deserialized and added to the above array, then reloadData is called on the UICollectionView instance. This is where the app seems to stop doing anything, and after 20-30 seconds (or sometimes up to a minute), the delegate receives a call to numberOfSectionsInCollectionView. As soon as this call is received, the reload quickly completes.

I believe reloadData runs in its own thread, but I've tried starting it in a separate thread to ensure that it's not any of my code blocking the process. What could reloadData be doing while it appears stuck? Are there any intermediary delegate methods between reloadData and numberOfSectionsInCollectionView that I should be implementing? I thought that is the first method to be called following a reload as far as an iOS developer is concerned.

If anyone could tell me what may be going wrong, fantastic, but I'd also appreciate any tips on how to debug this!

like image 940
Merott Avatar asked Dec 12 '22 21:12

Merott


1 Answers

Never do UIKit stuff from a background thread. So make sure reloadData is called on the main thread and don't modify the backing array from a background thread.

like image 91
Felix Avatar answered Jan 08 '23 08:01

Felix