title: UIPageViewController & API Calls in ContentViewControllers
body: <p>I've implemented a UIPageViewController which contains a variable number of UIViewControllers that each call an API in their <code>viewDidLoad</code> method. </p>
  
  <p>I'm having a few problems due to the "known issue" of the <code>viewControllerBeforeViewController</code> and <code>viewControllerAfterViewController</code> data source methods being called twice. e.g When swiping right, the next and one after next view controllers are initialised. This in turn means each initialised view controller's API calls are made. </p>
  
  <p>I only want the next view controller to be initialised and hence only that one API call to be made. I had thought about moving the API call to the <code>viewWillAppear</code> although would prefer if just the next view controller were initialised.</p>
  
  <p>Is there any way to ensure the data source methods are only called once?</p>
  
  <p>Thanks :)</p>
  
  <pre><code>func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let currentIndex = self.pages.indexOf(viewController as! LocationViewController)!
      if currentIndex == self.pages.count-1 {
          return nil
      }
      let nextIndex = currentIndex + 1
      return self.pages[nextIndex]
  
  }
  
  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let currentIndex = self.pages.indexOf(viewController as! LocationViewController)!
      if currentIndex == 0 {
          return nil
      }
      let previousIndex = currentIndex - 1
      return self.pages[previousIndex]
  
  }
  
  func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
      self.pendingIndex = self.pages.indexOf(pendingViewControllers.first! as! LocationViewController)
  }
  
  func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
      if completed {
          self.currentIndex = self.pendingIndex
          if let index = self.currentIndex {
              self.pageControl.currentPage = index
          }
      }
  }
  </code></pre>