big data in UIPageviewcontroller cause problem to the performance

Viewed 46

I have a UIPageViewControler which perform very well but in a lot of code!.

more than 4000 line and i know its big performance problem but i dont fegure a way to split the code into subclasses

the UIpageViewcontroler contain over 350 pages and well grow more becuse the app give the user a data each day !

at the beginning no problem but when the app grow in code the App become very old man trying to stand up !

can you guve me an idea how to make the code simpler ?

here is what i did :

at the main page view (TestPage) i added these variables :

var TodayDate: String?
var data1 : String? , data2: String?
var data3: String? , data4: String?
var data5: String? , data6: String?
var imagee: UIImage? 

at the UIpageViewcontroller i create an ViewController and added the data in previous variables:

let vc1 = self.storyboard?.instantiateViewController(withIdentifier: "page1")as! TestPageVC
    vc1.TodayDate = "06.09.2022"
    vc1.data1 = "welcome"
    vc1.data2 = "The first data..."
    vc1.data3 = "The first data..."
    vc1.data4 = "The first data..."
    vc1.data5 = "The first data..."
    vc1.data6 = "The first data..."
    vc1.imagee = UIImage(named: "0001")

let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "page1")as! TestPageVC
    vc2.TodayDate = "07.09.2022"
    vc2.data1 = "welcome"
    vc2.data2 = "The second data..."
    vc2.data3 = "The second data..."
    vc2.data4 = "The second data..."
    vc2.data5 = "The second data..."
    vc2.data6 = "The second data..."
    vc2.imagee = UIImage(named: "0002")

let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "page1")as! TestPageVC
    vc3.TodayDate = "08.09.2022"
    vc3.data1 = "welcome"
    vc3.data2 = "The third data..."
    vc3.data3 = "The third data..."
    vc3.data4 = "The third data..."
    vc3.data5 = "The third data..."
    vc3.data6 = "The third data..."
    vc3.imagee = UIImage(named: "0003")

//... until vc350+ whech is very big 

     

after that i created an array of all the VC created before:

arraypage1.append(contentsOf:[vc1, vc2, vc3])

to make the viewcontroller appears in the exact date, i created another array for date :

arraytime = ["06.09.2022","07.09.2022","08.09.2022"]

to make this happen i create an empty array called arraypage2 and only append today and previous days data :

    let Today = theDate.convertedDigitsToLocale(Locale(identifier: "EN"))
    for (index, element) in arraytime.enumerated() {
        if Today == element {

            let arraySlice = arraytime.prefix(index+1)
            let newArray = Array(arraySlice)
            for (index5, _) in newArray.enumerated() {
                arraypage2.append(contentsOf:[arraypage1[index5]])
            }
            let otherVC = arraypage2[index]
            self.setViewControllers([otherVC], direction: .forward, animated: false, completion: nil)
        }
        
        
    }

the problem is :

to load all these data evey time the UIPageViewcontroler appears is very unhelthy to the memory which cause the iPhone overheat and crash

when scroll the page before and go through over 350+ pages aloso cuse the same problem

how can i make this clean and functional without memory leak ?

sorry for my english and for this long issue

1 Answers

You should only set the initial time with 1 to 3 VCS inside pager , then you need to implement dataSource and delegate to take care of previous and next VCS

   pager.delegate = self
   pager.dataSource = self

extension YourVC : UIPageViewControllerDelegate , UIPageViewControllerDataSource {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        let vc = viewController as! ExampleVC  
        
        if vc.pageNumber > 1 {
            currentPage = vc.pageNumber - 1
            return createPage() // returns a vc with currentPage
             
        }
      
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        let vc = viewController as! ExampleVC
        
        if vc.pageNumber < 335 {
            currentPage = vc.pageNumber + 1
            return createPage() // returns a vc with currentPage
             
        }
        
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController,
                            didFinishAnimating finished: Bool,
                            previousViewControllers: [UIViewController],
                            transitionCompleted completed: Bool) {
        if completed {
           
        }
    }
}

 
Related