title: Paginated API Calls with RxSwift
body: <p>I am starting my first RxSwift project for an iOS app and learning about reactive programming.</p>
  
  <p>So far, the idea is quite simple, the user searches for films matching the search bar text, this fires a request that populates a UITableView with the results. Using tutorials and examples found online I've managed to implement this bit without too much trouble.</p>
  
  <p>The tricky part comes when I'm trying to load the next page of results triggered by scrolling the bottom of the table view.</p>
  
  <p>Here is the code used so far:</p>
  
  <pre><code>public final class HomeViewModel: NSObject {
  
      // MARK: - Properties
  
      var searchText: Variable&lt;String&gt; = Variable("")
      var loadNextPage: Variable&lt;Void&gt; = Variable()
  
      lazy var pages: Observable&lt;PaginatedList&lt;Film&gt;&gt; = self.setupPages()
  
      // MARK: - Reactive Setup
  
      fileprivate func setupPages() -&gt; Observable&lt;PaginatedList&lt;Film&gt;&gt; {
          return self.searchText
              .asObservable()
              .debounce(0.3, scheduler: MainScheduler.instance)
              .distinctUntilChanged()
              .flatMapLatest { (query) -&gt; Observable&lt;PaginatedList&lt;Film&gt;&gt; in
                  return TMDbAPI.Films(withTitle: query, atPage: 0)
              }
              .shareReplay(1)
      }
  }
  </code></pre>
  
  <p>Here is what I have so far: The observable <code>pages</code> is binded to my table view in <code>HomeViewController</code> and its search bar text is binded to <code>searchText</code>.</p>
  
  <p>I'm using Alamofire to perform API calls behind the scenes, and <code>TMDbAPI.Films(withTitle: query)</code> returns an Observable of paginated lists.</p>
  
  <p>Here is my model structure <code>PaginatedList</code> </p>
  
  <pre><code>public struct PaginatedList&lt;T&gt; {
  
      // MARK: - Properties
  
      let page: Int
      let totalResults: Int
      let totalPages: Int
      let results: [T]
  
      // MARK: - Initializer
  
      init(page: Int, totalResults: Int, totalPages: Int, results: [T]) {
          self.page = page
          self.totalResults = totalResults
          self.totalPages = totalPages
          self.results = results
      }
  
      // MARK: - Helper functions / properties
  
      var count: Int { return self.results.count }
  
      var nextPage: Int? {
          let nextPage = self.page + 1
          guard nextPage &lt; self.totalPages else { return nil }
          return nextPage
      }
  
      static func Empty() -&gt; PaginatedList { return PaginatedList(page: 0, totalResults: 0, totalPages: 0, results: []) }
  }
  
  extension PaginatedList {
  
      // MARK: - Subscript
  
      subscript(index: Int) -&gt; T {
          return self.results[index]
      }
  }
  </code></pre>
  
  <p>I am now looking for a reactive way to hook my <code>loadNextPage</code> variable to the observable of paginated lists in a way that it would trigger a request for the next page.
  And when the search bar text changes it would reset the pagination to 0.</p>
  
  <p>I believe the use of the operators <code>scan</code> and <code>concat</code> would be needed but I'm still not sure how...</p>
  
  <p>Any suggestions on how to achieve this would be much appreciated...</p>