title: SwiftUI's onAppear() and onDisappear() called multiple times and inconsistently on Xcode 12.1
body: <p>I've come across some strange behavior with SwiftUI's <code>onAppear()</code> and <code>onDisappear()</code> events. I need to be able to reliably track when a view is visible to the user, disappears, and any other subsequent appear/disappear events (the use case is tracking impressions for mobile analytics).</p>
  <p>I was hoping to leverage the <code>onAppear()</code> and <code>onDisappear()</code> events associated with swiftUI views, but I'm not seeing consistent behavior when using those events. The behavior can change depending on view modifiers as well as the simulator on which I run the app.</p>
  <p>In the example code listed below, I would expect that when <code>ItemListView2</code> appears, I would see the following printed out in the console:</p>
  <pre><code>button init
  button appear
  </code></pre>
  <p>And on the iPhone 8 simulator, I see exactly that.</p>
  <p>However, on an iPhone 12 simulator, I see:</p>
  <pre><code>button init
  button appear
  button disappear
  button appear
  </code></pre>
  <p>Things get even weirder when I enable the <code>listStyle</code> view modifier:</p>
  <pre><code>button init
  button appear
  button disappear
  button appear
  button disappear
  button appear
  button appear
  </code></pre>
  <p>The iPhone 8, however remains consistent and produces the expected result.</p>
  <p>I should also note that in no case, did the Button ever seem to disappear and re-appear to the eye.</p>
  <p>These inconsistencies are also not simulator only issues, i noticed them on devices as well.</p>
  <p>I need to reliably track these appear/disappear events. For example I'd need to know when a cell in a list appears (scrolled into view) or disappears (scrolled out of view) or when, say a user switches tabs.</p>
  <p>Has anyone else noticed this behavior?  To me this seems like a bug in SwiftUI, but I'm not certain as I've not used SwiftUI enough to trust myself to discern a programmer error from an SDK error. If any of you have noticed this, did you find a good work-around / fix?</p>
  <p>Thanks,</p>
  <ul>
  <li>Norm</li>
  </ul>
  <pre><code>// Sample code referenced in explanation
  // Using Xcode Version 12.1 (12A7403) and iOS 14.1 for all simulators
  import SwiftUI
  
  struct ItemListView2: View {
  
      let items = [&quot;Cell 1&quot;, &quot;Cell 2&quot;, &quot;Cell 3&quot;, &quot;Cell 4&quot;]
  
      var body: some View {
          ListingView(items: items)
      }
  }
  
  private struct ListingView: View {
      let items: [String]
  
      var body: some View {
          List {
              Section(
                  footer:
                      FooterButton()
                      .onAppear { print(&quot;button appear&quot;) }
                      .onDisappear { print(&quot;button disappear&quot;) }
              ) {
                  ForEach(items) { Text($0) }
              }
          }
  //      .listStyle(GroupedListStyle())
      }
  }
  
  private struct FooterButton: View {
      init() {
          print(&quot;button init&quot;)
      }
      var body: some View {
          Button(action: {}) { Text(&quot;Button&quot;)  }
      }
  }
  </code></pre>