2

I have a TabView with a PageTabViewStlye(). In my personal project its get called tons of times, and I've reproduced with this little example. This is a big problem for me cause I have a big view where it calls the network, so I expect to onAppear just be called one time. Any ideas? If I remove the tabViewStyle it just works.

struct ContentView: View {

var body: some View {
    TabView() {
        Text("random")
            .onAppear {
                print("printed multiple times, 5 in a row")
            }
        Text("Other")
    }.tabViewStyle(PageTabViewStyle())
}

}

Godfather
  • 4,040
  • 6
  • 43
  • 70
  • *...expect to onAppear just be called one time...* - don't rely on that - manage your network calls manually (say with some delay, debounce, caching, etc.) – Asperi Oct 13 '20 at 13:32
  • so should I throtlle onAppear for example? and just allow 1/sec? Isn't weird? – Godfather Oct 13 '20 at 13:39
  • 1
    No it is not... by interface contract it only guaranties telling you that view is appeared, but not when, where, and how many times... it is not UIKit view with referenced one instance, it is value, might be recreated, might be not... it is reactive nature - you don't expect, you just react. – Asperi Oct 13 '20 at 13:45
  • @Asperi could you elaborate on this? https://gist.github.com/ivangodfather/8a95b37bd4944bb901df763b7bd1dc69 – Godfather Oct 13 '20 at 16:02
  • Have you solved it?I have the same problem: https://stackoverflow.com/questions/64641759/swiftui-pagetabview-in-ios14-2-will-recall-childview-onappear-method-many-times – wanbo Nov 08 '20 at 14:38
  • @Werb yes, don’t use beta. Use latest Xcode – Godfather Nov 08 '20 at 14:46
  • @Godfather I use the latest Xcode Version 12.1 (12A7403) but also have this problem, and I find it just Occur in iOS 14.2, 14.1 is ok... – wanbo Nov 08 '20 at 15:23
  • I manage to have a solution for both iOS 13 and 14 . Check this: https://stackoverflow.com/a/67346255/748243 – Fx. May 01 '21 at 12:40

0 Answers0