<?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 Mrigank Gupta on Medium]]></title>
        <description><![CDATA[Stories by Mrigank Gupta on Medium]]></description>
        <link>https://medium.com/@mrigankgupta_68367?source=rss-bd759b59dbca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*vx10tUr9zQLHmBlTK3cJLQ.png</url>
            <title>Stories by Mrigank Gupta on Medium</title>
            <link>https://medium.com/@mrigankgupta_68367?source=rss-bd759b59dbca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 16:40:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrigankgupta_68367/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[An easy way to Pagination or Infinite scrolling for TableView/CollectionView]]></title>
            <link>https://medium.com/@mrigankgupta_68367/an-easy-way-to-pagination-or-infinite-scrolling-for-tableview-collectionview-56863f8f63dd?source=rss-bd759b59dbca------2</link>
            <guid isPermaLink="false">https://medium.com/p/56863f8f63dd</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Mrigank Gupta]]></dc:creator>
            <pubDate>Fri, 29 Nov 2019 19:19:23 GMT</pubDate>
            <atom:updated>2020-01-05T21:32:32.869Z</atom:updated>
            <content:encoded><![CDATA[<h3>An easy way to Infinite scrolling for TableView/CollectionView</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ksqetoj5e76WpJHuUcMAQg.gif" /><figcaption>Amazing gif by <a href="https://dribbble.com/claudiosc8"><strong>Claudio Scotto</strong></a></figcaption></figure><p><a href="https://en.wikipedia.org/wiki/Pagination">Pagination</a>, also known as paging, is the process of dividing a document into discrete pages, either electronic pages or printed pages.”</p><p>It is the most common technique to manage large data set at the server/client-side to distribute in chunks called pages. In today’s time, Social media client apps improvised this by inventing “Infinite scroll”.</p><p>Infinite scrolling allows users to load content continuously, eliminating the need for user’s explicit actions. The app loads some initial data and then load the rest of the data when the user reaches the bottom of the visible content. This data is divided into pages.</p><h4>A framework for Infinite scrolling?</h4><p>As apps size and content, are increasing day by day, we need to implement infinite scroll in more than one place in our app. Although implementing an infinite scroll is not very tricky but it is a repetitive job and creates redundant code inside TableView/CollectionView and yes it breaks the “DRY” principle too.</p><p>Whenever we have repeated code, there is always a chance of introducing a bug. I lately implemented infinite scroll in some of the apps and I felt that at times you need to implement this in quite a few places, typically if you are working on a large e-commerce app. I was looking for a solution, a framework that does this job for me.</p><p>I was not able to find a non-intrusive and easy to integrate solution where I don’t need to sub-class my ViewController or use a special kind of tableView/collectionView for this and It can work with my network stack. In short, I don’t want to create direct dependency on a framework/library all over in my project code.</p><p>So I thought of giving it a try.</p><p>Initial, I was not clear on <em>what</em> and <em>how</em> part. My refactored efforts were scattered all over the place. While I ended up having a non-intrusive solution (what I played out as a requirement), I learned an important lesson after making mistakes.</p><ul><li><strong>Zoom out as far as you can until you are clear <em>who</em> is playing <em>what</em> role.</strong></li></ul><p>As a first step, we usually concentrate on the problem and sometimes ignore the full aerial view ( when I say full aerial view, sometimes it involves objects/classes which are far from the problem space).</p><p>Initially, I was relentlessly trying to decouple/refactored code without identifying my interface and participant objects or classes. You can only do “<em>Separation of Concern</em>” when you identify <em>which</em> class is going to do <em>what</em>.</p><ul><li><strong>What it takes to use your framework/Library</strong></li></ul><p>When I started refactoring my code for the library, I had not laid out my interfaces well. Generally, this is a common problem. We usually don’t develop solutions that are being designed for a larger audience but used within few use cases. It really opens our eyes when we start thinking in this direction. And yes, this should be the second step when we develop a framework.</p><h4><strong>What part:</strong></h4><ol><li>We need an Inter-actor/ Controller which</li></ol><ul><li>Maintains a number of visible rows</li><li>Load next page at the end of the scroll if required</li><li>Maintain the current page count and update all of these when it gets the next page data array from the network.</li></ul><p>2. To accomplish all of this, It needs some supporting function on top of tableView/collectionView</p><p>3. An interface between a network stack.</p><h4><strong>How Part:</strong></h4><p>The trickiest part was the interfacing with the network. It took some time to figure out how to separate it from the dependency of the network stack.</p><p>Most of the pagination responses have the common structure which comprises total Page count, current page and an array of elements. With this assumption, A <strong><em>struct</em></strong> can be defined for all responses over generic parameter Type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cfdae2d210e0448342d8fd1ededa484/href">https://medium.com/media/7cfdae2d210e0448342d8fd1ededa484/href</a></iframe><p>Now with this PageInfo struct in place, We have a generic way of giving data back to Inter-actor/Controller for updating the results after getting it from web service. This solves one problem of the puzzle, another piece of a puzzle is to indicate Network stack to download next pages data as and when required.</p><p>So I designed an interface between Pageable and network stack which has two methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f547624ac9c1825d2ec24fd1a7acfc0/href">https://medium.com/media/5f547624ac9c1825d2ec24fd1a7acfc0/href</a></iframe><p>Function <em>LoadPage</em> is called by <em>PageInteractor</em> when it figures out that there is more data to load. A typical implementation can be</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ad58a5ec1d53f700c285df89da2ceee/href">https://medium.com/media/9ad58a5ec1d53f700c285df89da2ceee/href</a></iframe><p>First method <strong><em>func</em></strong><em> loadPage(</em><strong><em>_</em></strong><em> page: Int) -&gt; Void</em> indicates <em>when</em> to request <em>which</em> page. It loads the next set of data, the loaded data needs to be sent to PageInteractor for updating results.</p><p>Now calling <em>PageInteractor</em>, is a requirement that can be missed as API is not enforcing it.</p><h4>Completion blocks are here!</h4><p>Completion block helps in situations like this. So we can update an interface with completion block. As <em>PageInfo&lt;T&gt; </em>operates on the generic parameter, This requires to create a generic interface to adopt by.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae1aa2abdbbb90804996ea8ca654d856/href">https://medium.com/media/ae1aa2abdbbb90804996ea8ca654d856/href</a></iframe><p>This creates a clear requirement for the user to send data back to Page Interactor as well as gives a clean way to separate <a href="https://github.com/mrigankgupta/Pageable"><strong>Pageable</strong></a> library from your networking code.</p><h4><strong>Power of KeyPath:</strong></h4><p><strong>Pageable </strong>gives a feature where duplicate entries from a server can be avoided at the client-side if somehow new entries are being added dynamically at the server end. More can be found <a href="https://github.com/mrigankgupta/Pageable#advance-usage">here</a>.</p><p>To use this feature your pagination data should have a unique Id in response array so that duplicates can be identified. To make this feature usable, It requires a user to give the power to provide a generic way to identify that field.</p><p>Earlier due to the inability to provide this information to <em>PageInteractor</em>, the User needs to implement interface <em>PageDataSource</em> methods that check items in the dictionary and then add in the final array to avoid duplication. This is a very redundant task for the user and ideally handled by <em>PageInteractor</em> only.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb27e8ce58b02811661e53dd3793fe0e/href">https://medium.com/media/bb27e8ce58b02811661e53dd3793fe0e/href</a></iframe><blockquote><strong><em>KeyPath</em></strong><em> are a way to reference properties without invoking them. It can be composed to make different path and used to get/set underlying properties.</em></blockquote><p>With the KeyPath passed in the initializer of <em>PageInteractor</em>, the implementation of both methods can move entirely in <em>PageInteractor</em>. This will relieve the user from not implementing <em>PageDataSource</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/266e6c705999ce0783acfbe9d4ad3f32/href">https://medium.com/media/266e6c705999ce0783acfbe9d4ad3f32/href</a></iframe><p>With default implementation embedded in <em>PageInteractor</em>, All it takes is to provide KeyPath of the unique data type to <em>PageInteractor</em> to use this feature.</p><blockquote><strong>let</strong> pageInteractor: <strong>PageInteractor</strong>&lt;<strong>UserModel</strong>, <strong>Int</strong>&gt; = <strong>PageInteractor</strong>(firstPage: firstReqIndex, service: service, keyPath: \UserModel.id)</blockquote><h4><strong>Conclusion</strong></h4><p>While developing infinite scrolling as a framework, was fun and turned out to be an easy solution to integrate with few steps to follow. I learned an important lesson on <em>what </em>part. There is always a room of improvement when you look back to your solution. I did a few iterations while finalizing interface for Pageable and now I feel next time I can avoid a few of those. You can find the library <a href="https://github.com/mrigankgupta/Pageable">here</a> at Github, feel free to give it a try/feedback!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56863f8f63dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>