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.
Here is a screenshot of the Time Profiler:
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?
Other observations I made:
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.
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?