I have a problem with big memory leaks when I use UIPageViewController. I have a view and I scroll it. I see that memory grows very fast.
i have an array of ViewController (about 350+ vc), the UIPageViewcontroller shows the every VC in a page, the problem is :
to append 350+ Viewcontroller in array is cause memory to grow up fast so is there a way to append VC in array by viewController Before and viewController After method when scrolling ?
so the array will always be 2 index for example
arraypage1 = [vc1,vc2,vc3]
when scrolling to the next page arraypage1 well be [vc2,vc3,vc4] so the index 0 remove but a new page will add at index 2
here is what i did so far :
at TestVC :
var TodayDate: String?
var data1 : String? , data2: String?
var data3: String? , data4: String?
var data5: String? , data6: String?
var imagee: UIImage?
at the UIpageViewcontroller :
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")
let vc4 = self.storyboard?.instantiateViewController(withIdentifier: "page1")as! TestPageVC
vc4.TodayDate = "08.09.2022"
vc4.data1 = "welcome"
vc4.data2 = "The fourth data..."
vc4.data3 = "The fourth data..."
vc4.data4 = "The fourth data..."
vc4.data5 = "The fourth data..."
vc4.data6 = "The fourth data..."
vc4.imagee = UIImage(named: "0004")
let vc5 = self.storyboard?.instantiateViewController(withIdentifier: "page1")as! TestPageVC
vc3.TodayDate = "08.09.2022"
vc3.data1 = "welcome"
vc3.data2 = "The fifth data..."
vc3.data3 = "The fifth data..."
vc3.data4 = "The fifth data..."
vc3.data5 = "The fifth data..."
vc3.data6 = "The fifth data..."
vc3.imagee = UIImage(named: "0005")
arraypage1.append(contentsOf:[vc1, vc2, vc3])
// here i want add vc4 and remove vc1 when go to next page
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let currentIndex = arraypage2.firstIndex(of: viewController) else {
return nil
}
UserDefaults.standard.removeObject(forKey: "emptyView1")
let previousIndex = currentIndex - 1
guard previousIndex >= 0 else {
return nil
}
return arraypage2[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let currentIndex = arraypage2.firstIndex(of: viewController) else {
return nil
}
let afterIndex = currentIndex + 1
let afterIndex3 = currentIndex + 2
if afterIndex3 == arraypage2.count {
UserDefaults.standard.set("next", forKey: "emptyView1")
}
guard afterIndex < arraypage2.count else {
return nil
}
return arraypage2[afterIndex]
}
