UICollectionView stuck in reloadData

此生再无相见时 提交于 2019-12-07 08:13:05

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/13208480/uicollectionview-stuck-in-reloaddata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!