🆕 iOS 15 brings radical new cell prefetching behavior to UICollectionView and (for the first time ever!) UITableView — this is a quantum leap forward for smooth scrolling performance and power efficiency.

Here's a thread with some key takeaways ⬇️
What's the big deal?

In iOS 15, the system intelligently identifies spare time between frames, and uses that to start work on the next cell, giving you up to 2x time to prepare each cell!

Best part: just build your app with the iOS 15 SDK, and you get it automatically for free!
The new cell prefetching behavior "just works" because it doesn't change the threading model or API contract: cells are still requested the same way as before, just with slightly different timing; it's all about better scheduling and utilization of time on the main thread.
With more time to finish all the work that goes into preparing a cell — loading your model object, formatting, image decoding, text measurement, layout & drawing, etc — you're much more likely to avoid hitches (frame drops).

If this sounds too good to be true, I don't blame you!
So what's the catch?

The main thing to keep in mind with cell prefetching is how it affects cell lifecycle. For example:
• Cells are requested earlier before displaying (and may never display)
• Cells are cached for awhile after displaying (to display again for the same item)
When using cell prefetching, watch out for cases where the data for a cell changes after the cell has been prefetched, but before it becomes visible (thus it won't be included in visibleCells).

The best way to handle this is using the new reconfigure API:
Also look out for existing code which assumes your data source cellForItemAt/cellForRowAt method (or diffable cell provider) is always followed immediately by willDisplayCell.

Similarly, you can get more than one pair of willDisplay + didEndDisplaying for the same prepared cell.
Don't forget that cell prefetching (automatic, with new behavior in iOS 15) is separate from data source prefetching, which is opt-in (by setting a prefetchDataSource on the collection/table view since iOS 10) and gives you a "heads up" to start fetching data for upcoming items.
To learn more about how the new iOS 15 cell prefetching behavior works under the hood, see illustrations of the cell lifecycle before/after it, and hear tips & tricks for maximizing scrolling performance, check out this #WWDC21 video (and sample code):
developer.apple.com/videos/play/ww…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Tyler Fox

Tyler Fox Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @smileyborg

13 Jun
iOS 15 introduces a new way for you to conveniently update content displayed in existing cells in UICollectionView and UITableView: reconfigure.

When and why should you use reconfigure? How is it different from reloading an item or row?

Let’s dive in with a quick thread.
You can think of reconfigure as a lighter-weight version of reload.

Reload: replaces the existing cell with a new cell.

Reconfigure: allows you to directly update the existing cell.

Because reconfigure doesn’t request/create a new cell, it’s significantly more efficient!
How does reconfigure work?

For each item/row you reconfigure:

- If there’s no existing cell, it’s a no-op!
- Otherwise, the collection/table view calls your cell provider again, but with special behavior to return the *existing* cell when you dequeue one for that index path.
Read 10 tweets
9 Jun
A small but very powerful API addition in iOS 15 is the new configurationUpdateHandler property on UICollectionViewCell, UITableViewCell, UITableViewHeaderFooterView, and even UIButton.
developer.apple.com/documentation/…
TL;DR: you can set a closure which is called anytime the view's state changes.

If you're familiar with the updateConfiguration(using state:) method introduced last year in iOS 14, this is a closure-based version of it — so you don't need a subclass just to override that anymore!
This handler makes it really easy to customize the configuration & appearance of your cells/headers/buttons for different states, directly inline.

It's a great place to apply a new configuration, but you can update any other properties of the cell/view in the handler, too!
Read 7 tweets
16 Oct 20
It’s a good question. There are a number of reasons why these properties are marked as “to be deprecated” in a future release.

Here’s a short thread to explain some of those.
For starters, there’s well over a decade of implementation behind these properties and the different cell styles, with layers upon layers of complexity to preserve binary compatibility with all the apps using them. (Old apps in the App Store need to keep working without updates!)
Unfortunately, because these built-in cell textLabel/imageView properties expose the entire API surface of UILabel/UIImageView, that means apps are doing all sorts of unexpected things to these views that weren’t intended to be supported (but aren’t explicitly disallowed, either)
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(