0

I know how to load a different page than page 1 in a UIPageViewController. The problem is that my UIPageViewController is 150 pages long and when I try to load Page 100, it takes up to 8 sec to do the task. Any suggestion in how to speed up the process?

here is how I currently do it:

let savedPage = self.load(page: bookmarkPageNumber, pageViewController: self)!
self.setViewControllers([savedPage], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
edare
  • 107
  • 1
  • 13
  • are you getting any memory warnings? are all your pages similar? – Fonix Jan 31 '17 at 01:55
  • No memory warnings, I am displaying only text, like in a book. It all works well, it is just taking long to load the page when it is like around the 100 pages. – edare Jan 31 '17 at 05:03
  • does it slowly take longer and longer with each page load, or does it suddenly take a really long time at page 100? – Fonix Jan 31 '17 at 06:01
  • No, only when I jump straight to high number pages like page 100, 101, 102 ecc. My problem is the following: I have a long text that I display on a UIPageVIewController as if it was a book. When the user leave the UIPageVIewController I save the page they arrived as if it was a bookmark, in coredata. Once they enter the UIPageViewController again I retrieve the page saved and jump to the correct page. But if I have a lot of pages the UIPageVIewController behaves very slowly... – edare Feb 01 '17 at 05:43
  • it sounds like its loading all the pages before the one you jump to, causing it to do too much work at the same time on the main thread, can you test somehow to see that it is only loading the relevant page? – Fonix Feb 01 '17 at 05:57
  • it is exactly like you say, it is loading all pages before jumping to the correct one. How can I load only the relevant page and all the others afterwards? – edare Feb 02 '17 at 03:26
  • Im not sure, but a quick google revealed [this answer](http://stackoverflow.com/a/18602186/1219956) – Fonix Feb 02 '17 at 03:30

1 Answers1

0

In your case you should use UIPageViewControllerDataSource (https://developer.apple.com/reference/uikit/uipageviewcontrollerdatasource) instead of setViewControllers(..)

Example:

yourPageController.dataSource = self

And add this methods to class that contains your yourPageController. Don't forget to mark this class as UIPageViewControllerDataSource.

    //MARK: - UIPageViewControllerDataSource

    func pageViewController(pageViewController: UIPageViewController,
        viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
            guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
                return nil
            }

            let previousIndex = viewControllerIndex - 1

            guard previousIndex >= 0 else {
                return nil
            }

            guard orderedViewControllers.count > previousIndex else {
                return nil
            }

            return orderedViewControllers[previousIndex]
    }

    func pageViewController(pageViewController: UIPageViewController,
        viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
            guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
                return nil
            }

            let nextIndex = viewControllerIndex + 1
            let orderedViewControllersCount = orderedViewControllers.count

            guard orderedViewControllersCount != nextIndex else {
                return nil
            }

            guard orderedViewControllersCount > nextIndex else {
                return nil
            }

            return orderedViewControllers[nextIndex]
    }
Nikita Gaidukov
  • 771
  • 4
  • 14
  • Thanks for responding, but I am actually using the UIPageViewControllerDataSource and the functions above. My problem is the following: I have a long text that I display on a UIPageVIewController as if it was a book. When the user leave the UIPageVIewController I save the page they arrived as if it was a bookmark, in coredata. Once they enter the UIPageVIewController again I retrieve the page saved and jump to the correct page. But if I have a lot of pages the UIPageVIewController behaves very slowly... – edare Feb 01 '17 at 05:41
  • @Giovanni I see. In your case i would probably use UICollectionView with paging enabled. It will allow you to use as many pages as you want without slowdown. And when you need to jump to a certain page you simply call scrollToItemAtIndexPath with animated = false. – Nikita Gaidukov Feb 02 '17 at 07:45