UIPageViewController with dynamic DataSource pages gets repeated
<p>When I do fast swipe between pages sometimes I get duplicate pages. My index counting may be wrong, but I tried everything and always get the same. </p>

<p><strong>DetailedProfileViewController.swift</strong></p>

<pre><code>class DetailedProfileViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var currentProfileIndex : Int?
    var nextProfileIndex :Int?
    var data: Main?

    var mainPageView : UIPageViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let profile = ProfileViewController()
        profile.profile = currentProfile()
        let arraysOfViews = [profile]

        mainPageView = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])
        mainPageView.setViewControllers(arraysOfViews, direction: .forward, animated: true, completion: nil)
        mainPageView.dataSource = self
        mainPageView.delegate = self

        addChildViewController(mainPageView)
        view.addSubview(mainPageView.view)
        mainPageView.didMove(toParentViewController: self)

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -&gt; UIViewController? {
        if currentProfileIndex == (data?.filteredProfiles.count)!-1 { return nil }
        nextProfileIndex = abs((currentProfileIndex! + 1) % (data?.filteredProfiles.count)!)
        let profile = ProfileViewController()
        profile.profile = data?.filteredProfiles[nextProfileIndex!]
        return profile
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -&gt; UIViewController? {
        if currentProfileIndex == 0 { return nil }
        nextProfileIndex = abs((currentProfileIndex! - 1) % (data?.filteredProfiles.count)!)
        let profile = ProfileViewController()
        profile.profile = data?.filteredProfiles[nextProfileIndex!]
        return profile
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed == true {
          currentProfileIndex = nextProfileIndex
        }
    }
}
</code></pre>

<p>I'm not setting all of View Controllers in viewDidLoad because it could be hundreds of them..</p>