UICollectionView is very slow when inserting or deleting many items

丶灬走出姿态 提交于 2020-01-03 19:42:45

问题


I'm trying to insert and delete a large amount of items (say, 20,000) from a collection view, and the operation takes a very long time.

The test fixture I created is composed of the following:

  • UICollectionView with no configuration besides a data source.
  • Default UICollectionViewFlowLayout.
  • Data source that returns either 10K or 30K items depending on a BOOL variable.
  • Button to toggle that variable. When set to YES, 20K items are being added to the data source (just by changing numberOfItemsInSection:) and insertItemsAtIndexPaths: with 20K items. When set to NO, deleteItemsAtIndexPaths: is called with 20K items.
  • Cell configuration in the data source does nothing besides dequeuing a default UICollectionViewCell and returning it.

Running this on simulator, which should be faster than any device, yields the following timings:

  • Insertion of 20K items: 220ms.
  • Deletion of the same 20K items: 1100ms.

This is, by all means, horribly slow, especially when performed on the main thread.

Here's a screenshot from instruments, showing the hotspots in UICollectionView's internal implementation (specifically, _computeItemUpdates):

I've noticed that the use of reloadData instead of inserting or updating the items is way faster (~20ms), probably because no animations are triggered so there's no need to compute the position of each item and section for animation purposes.

Any ideas on how to overcome this would be appreciated.


回答1:


Expand _computeItemUpdates. If anything it's calling is yours, then yes you can.

An example would be if you are using a custom layout, you could ask it to calculate the new positions on a background thread and then call insert/delete when that operation finishes.

You could also be smart about it and only call insert/delete for ranges that are currently visible, then after the rearrange animation finishes you could reloadData and it shouldn't look too different from a users perspective.



来源:https://stackoverflow.com/questions/29216412/uicollectionview-is-very-slow-when-inserting-or-deleting-many-items

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