<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dharmesh Patel on Medium]]></title>
        <description><![CDATA[Stories by Dharmesh Patel on Medium]]></description>
        <link>https://medium.com/@dharmesh.patel_95492?source=rss-573c51e3777f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*lLC4Yg4rstfJs_qc.jpg</url>
            <title>Stories by Dharmesh Patel on Medium</title>
            <link>https://medium.com/@dharmesh.patel_95492?source=rss-573c51e3777f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 12:40:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dharmesh.patel_95492/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Butter Smooth Performance of TableView and CollectionView with Prefetching API  iOS]]></title>
            <link>https://medium.com/globant/butter-smooth-performance-of-tableview-and-collectionview-with-prefetching-api-ios-428fcc46c56e?source=rss-573c51e3777f------2</link>
            <guid isPermaLink="false">https://medium.com/p/428fcc46c56e</guid>
            <category><![CDATA[prefetch]]></category>
            <category><![CDATA[tableviews]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[collection]]></category>
            <category><![CDATA[smooth]]></category>
            <dc:creator><![CDATA[Dharmesh Patel]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 15:20:36 GMT</pubDate>
            <atom:updated>2020-09-03T13:35:43.208Z</atom:updated>
            <content:encoded><![CDATA[<h3>Butter Smooth Performance of TableView and CollectionView with Prefetching API iOS</h3><h3>Choppy Scrolling</h3><p>The smooth scrolling meets the 60 frames per second (FPS) display constraint. so the application needs to be able to refresh its UI 60 times per second, it means each frame has about 16ms to render content. The system drops frames that take too long to show content.</p><p>In the choppy scrolling experience as the application skips some frames and moves onto the next frame. A possible reason for a dropped frame operation is a long-running and that’s blocking the main thread.</p><p>So you have to separate out long-running operations and move them to a background thread. This allows the user to handle any touch events. When the background operation completes, you can make any required UI updates, based on the operation, on the main thread.</p><p>The following shows the dropped frame scenario:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*tcMbYymwXZGw2mo0.png" /></figure><p>Once you move work to the background, things look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*syHCaIy7tobdSxF2.png" /></figure><p>You now have two concurrent threads running to improve your app’s performance.</p><h3>How prefetching works</h3><p>Even better if you can start fetching data before the cell had to display it?<br>So In iOS 10, Apple has introduced API for prefetching <strong>UITableView</strong> and <strong>UICollectionView. I</strong>n this story, I will explain what is it for and how to properly use prefetching for UITableView.</p><p>Prefetching is useful when we need to download large objects ( images or video files) from the remote or local memory. Prefetching also gives us lazy loading in accessing data models: we don’t need to obtain all the data models, but just need to download nearly about to display. prefetching reduces battery and CPU consumption. it gives better user experience and smoothness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*x8rgmeg6JAHvncdB.png" /></figure><p>Apple didn’t mention exactly how many rows outside of the visible cell will be prefetched.</p><ul><li><strong>tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])</strong> doesn’t get called for rows that are visible on the screen initially without scrolling.</li><li>Right after initially visible rows became visible, tableView<strong>(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])</strong> will be called, and the indexPaths variable contains around 7 to10 rows nearest to the visible area.</li><li>Depend on the scroll speed, the <strong>indexPaths</strong> in prefetch, the method will contain a different number of rows. On normal scrolling speed, usually, it will contain a single row. If you scroll at a fast speed, it will contain multiple rows.</li></ul><h3>Implement</h3><p>New property prefetchDataSource has been introduced in <strong>UITableView</strong> and <strong>UICollectionView</strong>:</p><pre><strong>@available</strong>(iOS 10.0, <strong>*</strong>)<br><strong>weak</strong> <strong>open</strong> <strong>var</strong> prefetchDataSource: <strong>UITableViewDataSourcePrefetching</strong>?</pre><p>We have to confirm the UITableViewDataSourcePrefetching protocol the same as UITableViewDataSource and UITableViewDelegate.</p><pre>Class ViewController: UITableViewDataSourcePrefetching {</pre><pre>}</pre><p>Bind view controller to table view prefetch datasource by programmatically or from storyboard (interface)</p><pre>tableView.prefetchDataSource = self</pre><p>OR</p><figure><img alt="You have to bind prefetchDataSource" src="https://cdn-images-1.medium.com/max/398/1*hA5eP_munUfwXrfbjF7jBg.png" /></figure><p>Apple’s introduced of prefetching API UITableViewDataSourcePrefetching protocol with two methods:</p><pre>Required method:</pre><pre><strong>public</strong> <strong>func</strong> <strong>tableView</strong>(_ tableView: <strong>UITableView</strong>, prefetchRowsAt indexPaths: <strong>[IndexPath]</strong>)</pre><pre>Optional method:</pre><pre><strong>optional</strong> <strong>public</strong> <strong>func</strong> <strong>tableView</strong>(_ tableView: <strong>UITableView</strong>, cancelPrefetchingForRowsAt indexPaths: <strong>[IndexPath]</strong>)</pre><h3>Example</h3><pre>func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {</pre><pre>    for indexPath in indexPaths {</pre><pre>       guard operations[indexPath] == nil else { return }</pre><pre>       if let loader = dataStore.downloadImage(indexPath.row) {</pre><pre>       operationQueue.addOperation(loader)<br>       operations[indexPath] = loader</pre><pre>      }<br>    }<br>}</pre><p>Cancel the downloading (in Progress data) when it no needs more:</p><pre>func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {</pre><pre>    for indexPath in indexPaths {</pre><pre>      if let loader = operations[indexPath] {</pre><pre>        loader.cancel()<br>        operations[indexPath] = nil<br>    }<br>  }<br>}</pre><p>Check the while data is already in operation in willDisplay methods.</p><pre>func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {</pre><pre>  guard let cell = cell as? ImageTableViewCell else { return }</pre><pre>cell.downloadImageView?.image = nil</pre><pre>  if let loader = operations[indexPath] {</pre><pre>     if let image = loader.image {<br>       cell.downloadImageView.image = image<br>     } else {<br>      loader.completeHandler = { [weak self, weak cell] image in<br>         guard self != nil else {return}<br>         cell?.downloadImageView.image = image</pre><pre>       }<br>     }</pre><pre>} else {</pre><pre>      if let loader = dataStore.downloadImage(indexPath.row) {<br>          operationQueue.addOperation(loader)<br>          operations[indexPath] = loader<br>          loader.completeHandler = { [weak self, weak cell] image in<br>          guard self != nil else {return}<br>          cell?.downloadImageView.image = image<br>      }<br>  }<br> }<br>}</pre><h3>Performance Check</h3><figure><img alt="Without Prefetching" src="https://cdn-images-1.medium.com/max/500/1*idOWB6m8a_iwhsKv93Emnw.png" /><figcaption>Without Prefetching 8 frame missed</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*NoqAhiFsT-_kXZfqU-x2cg.png" /><figcaption>With Prefetching 0 frame missed</figcaption></figure><h3>Conclusion</h3><p>The prefetch protocols allow us to lazy load data and the load’s data just before the row is presented, this allows for a good user experience also smoother scroll and loading process.</p><p>Reference from <a href="https://developer.apple.com/videos/play/wwdc2016/219/">APPLE WWDC</a><br>Reference Images from <a href="https://www.raywenderlich.com/7341-uicollectionview-tutorial-prefetching-apis">Raywenderlich</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=428fcc46c56e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/globant/butter-smooth-performance-of-tableview-and-collectionview-with-prefetching-api-ios-428fcc46c56e">Butter Smooth Performance of TableView and CollectionView with Prefetching API  iOS</a> was originally published in <a href="https://medium.com/globant">Globant</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>