rechsteiner / Parchment

A paging view with a highly customizable menu ✨
MIT License
3.35k stars 417 forks source link

How to fix pre-loading? I want to disable it #698

Open ruofeilyu opened 6 months ago

ruofeilyu commented 6 months ago

For example, If I have 3 pages, I found that each page's neighbors will also called viewDidload? how can I disable it to make it only load when I am showing(the first page)

rechsteiner commented 6 months ago

Hi @ruofeilyu! What's the motivation for disabling pre-loading? If you have some operation you only want to be called when showing the view I would recommend moving it into viewWillAppear/viewDidAppear. The problem with removing preloading is that you could potentially allocate a lot of instances, and they will all be allocated while scrolling. Imagine if a user is swiping slightly back-and-forth on the current page. Each time you hit the edge (which could happen a lot), you would need to allocate a whole new view controller.

ruofeilyu commented 6 months ago

My case is that I use parchment as a navigation tab, so you can think its tab's tab. I have already cache every controller so it will not be recreated,

My implementation is that I need to add my swiftUI in each viewcontroller viewdidLoad (the reason that I don't use parchment swiftUI is that it will reset my previous view if I have multiple page because every page is a struct, I don't want to reset every page, i wanna to maintain its original position, same reason if you switch the tab, you don't want your previous tab got reset), Ok, now my networking got triggered at each swiftUI onTask(), this will got triggered immediately on viewDidLoad, so here is that problem,I don't want to trigger networking if this page is not shown yet and I know I can trigger networking in viewWillAppear, but it's kinda break my design, I want my swiftUI doing everything rather than delegate some action to its wrapper view controller.

So that's the reason I want to disable preloading. I know it's a little complicated case....

aiKrice commented 5 months ago

@ruofeilyu My opinion on your usecase is that it's very special and maybe you are going wrong on either: