Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many view controllers - performance issue

I have an IOS app that lets user swipe through weeks of notes. Each week is a UIViewController - the swiping and switching between the view controllers are handled by a UIPageViewController.

On startup all the view controllers are initialised with their data.

When the user swipes I grap a view controller like this:

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    if let currentPageViewController = viewController as? SinglePageViewController {
      let currentIndex = currentPageViewController.index
      return self.weeks[currentIndex - 1]
    }
    return nil
  }

The app work flawless, until a use has many weeks, and thereby many view controllers. Startup time start to become an issue - and this will of cause only get worse as the weeks go on.

I've played around with initialising the each view controller when the user swipes. Like this:

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if let currentPageViewController = viewController as? SinglePageViewController {
          let currentIndex = currentPageViewController.index
          let newVC = SinglePageViewController()
          newVC.index = currentIndex - 1
          return newVC
        }
        return nil
      }

This approach works and the startup time is great - however, the swiping has now become sluggish and not smooth at all.

Can any one advise on how this issue can be resolved?

like image 788
Rasmus Avatar asked May 28 '26 17:05

Rasmus


1 Answers

The second method (creation on demand) is the correct way to do it. If the swipping gets slow then because you spend to much CPU time in init(), viewDidLoad, viewWillAppear, etc... Look at the initialization and move every CPU intensive task to background threads.

If you depend on data to create the ViewController then you have to preload the data in advance. But it is not needed to preload the data for more then 2 or 3 of them. If it takes to much time and you still run into performane problems then you have to accept that the device is not fast enough for your requirements and you have to present the user an loading indicator. (like UIActivityIndicator)

If you need help in optimizing the initialization then post your code.

like image 54
Darko Avatar answered May 30 '26 07:05

Darko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!