滚动时将部分插入 UICollectionView 时的性能问题 [英] Performance issue when inserting sections into UICollectionView while scrolling

查看:44
本文介绍了滚动时将部分插入 UICollectionView 时的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我使用默认的UICollectionViewFlowLayout.当我使用 insertSections 时,它确实需要一些时间.特别是如果用户滚动这会带来一些口吃.我正在使用自动布局,在我的自定义 UICollectionViewCell 上放置一个标签.我正在使单元格和我的补充视图(标题)出列.这里没什么特别的.

Basically I'm using the default UICollectionViewFlowLayout. When I use insertSections it does take some time. Especially if the user scrolls this brings some stutters. I'm using auto layout where I position a label on my custom UICollectionViewCell. I'm dequeuing the cells and my supplementary views (header). Nothing special here.

这是 Time Profiler 的屏幕截图:

您在此处可以看到 UICollectionViewFlowLayoutprepareLayout 确实消耗了可用 CPU 功率的三分之一.有没有可能加快这个速度?例如.为 layoutAttributesForElementsInRect:layoutAttributesForItemAtIndexPath:collectionViewContentSizeprepareLayout 编写自定义方法?这种方法看起来如何?

What you can see here is that prepareLayout of UICollectionViewFlowLayout does consume a third of the available CPU power. Is there a possibility to speed this up? E.g. writing custom methods for layoutAttributesForElementsInRect:, layoutAttributesForItemAtIndexPath:, collectionViewContentSize, prepareLayout? How would this methods does look like?

我所做的其他观察:

添加的部分越频繁,插入部分(大约 420 项)所需的 performBatchUpdates 时间就越长.我每次都添加相同数量的项目.我用仪器做了一些测试.出现卡顿时,CPU 使用率超过 100%.实际内存使用量正在增加,但我以大约 60 MB 结束我的测试.当时数据集中加载了大约 48.000 个单元格(大约 45 个始终在屏幕上).一旦加载了单元格,滚动就会流畅地工作(CPU 最多大约 100%,但只有很短的时间,用户不会注意到).在另一个测试中,分配的总字节数约为 1.6 GB,但持久性保持在较低水平.如果插入新部分,帧也会降为 0.

The more often sections are added the longer performBatchUpdates with inserting the sections (about 420 items) does take. I'm adding everytime the same amount of items. I made some tests with Instruments. The CPU Usage goes over 100% when the stutter occur. Real memory usage is increasing but I ended my test with about 60 MB. About 48.000 cells were loaded in the dataset at that time (about 45 always on screen). Once the cells are loaded the scrolling works fluently (CPU about 100% max. but only short periods of time, user doesn't notice). The total bytes of allocations was about 1.6 GB in another test, but the persistent stayed low. The frames also drop to 0 if new sections are inserted.

我试图放弃单元格的自动布局,为单元格使用不透明的背景颜色,为单元格使用剪辑边界,使用 shouldRasterize 等等.单元格不重叠,不应使用小数值(圆形宽度).我根本不使用阴影.我也试着把事情注释掉,看看有什么问题,但我认为它一定是必不可少的.我在模拟器和不同设备上进行了测试.

I tried to go away from auto layout for the cell, use opaque background color for the cells, use clip to bounds for the cell, use shouldRasterize and so on. The cells don't overlap and no fractional values should be used (rounded width). I don't use shadows at all. I also tried to comment things out to see what has fault, but I think it must be something essential. I tested in simulator as well as on different devices.

有没有人知道如何在插入节时提高性能?

Has anyone a clue how to increase the performance when inserting sections?

推荐答案

在您的图像附件的调用树中有很多未解析的符号.您可以尝试在仅包含 MonthView 应用程序的那些调用树函数之一中缩小性能问题的范围.

There are quite a few unresolved symbols in the call tree of your image attachment. You could try to narrow down the performance problem in one of those call tree functions that consists of only the MonthView app.

尝试检查 Hide System Libraries 并使用 Debug 作为构建配置,这样您就可以观察这些符号并导航到您应用中的特定代码.

Try to check Hide System Libraries and use Debug as a build configuration, so you can observe those symbols and navigate to a specific code within your app.

一旦您清楚您的集合视图数据源或委托不会导致 UICollectionView 框架级别的性能问题,那么您就可以努力寻找一种方法来最小化此类集合查看部分更新.

Once you are clear that your collection view data source or delegate doesn't contribute to the performance issues at the UICollectionView framework level, then you could spend your effort to find a way to minimize such collection view section updates.

也许您的单元格自动布局配置非常复杂,您可以简化它吗?

Perhaps your cell auto layout configuration is quite complex, and you can probably simplify it?

尽管如果不实际查看您的集合视图单元格所具有的内容,则很难理解.

Although it's hard to understand without actually looking at what your collection view cell is having.

这篇关于滚动时将部分插入 UICollectionView 时的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆