简体   繁体   English

UICollectionView卡在reloadData中

[英]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. 我遇到了[UICollectionView reloadData] ,它在我指定的委托上调用numberOfSectionsInCollectionView之前似乎已经卡住了很长一段时间。 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 (ie 0) and immediately starts to download the data for the array from a web service. 应用程序以立即可见的UICollectionView开始,并在委托上调用numberOfSectionsInCollectionView ,它返回空数组中的项目数(即0),并立即开始从Web服务下载数组的数据。 Once the download is complete, the data is deserialized and added to the above array, then reloadData is called on the UICollectionView instance. 下载完成后,数据被反序列化并添加到上面的数组中,然后在UICollectionView实例上调用reloadData 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 . 这是应用似乎停止做任何事情的地方,并且在20-30秒(或有时长达一分钟)之后,委托接收对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. 我相信reloadData在它自己的线程中运行,但我尝试在一个单独的线程中启动它,以确保它不是我的任何代码阻止进程。 What could reloadData be doing while it appears stuck? 什么可以reloadData在看起来卡住时做什么? Are there any intermediary delegate methods between reloadData and numberOfSectionsInCollectionView that I should be implementing? 在我应该实现的reloadDatanumberOfSectionsInCollectionView之间是否有任何中间委托方法? I thought that is the first method to be called following a reload as far as an iOS developer is concerned. 我认为这是第一个在重新加载后调用的方法,就iOS开发人员而言。

If anyone could tell me what may be going wrong, fantastic, but I'd also appreciate any tips on how to debug this! 如果有人能告诉我可能出错的地方,太棒了,但我也很感激有关如何调试这个的提示!

Never do UIKit stuff from a background thread. 永远不要从后台线程做UIKit的东西。 So make sure reloadData is called on the main thread and don't modify the backing array from a background thread. 因此,请确保在主线程上调用reloadData,并且不要从后台线程修改后备阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM