title: SwiftUI @AppStroage - 'Simultaneous accesses to *, but modification requires exclusive access'
body: <p>In my project I was dealing with this issue.  After many hours, I was able to track down the causes and reduce them into a small demo, but do not understand the real issue. As you can see in the below code there are many things at play here, and the bug doesn't surface if any of the &quot;safe&quot; alternatives mentioned below are used.  Is this a Swift Memory Access issue, or a SwiftUI bug?</p>
  <p>Steps to reproduce:</p>
  <ol>
  <li>Copy the below code into a project</li>
  <li>Build on an iPad mini</li>
  <li>Tap the back button</li>
  </ol>
  <hr />
  <p>Crash</p>
  <pre><code>import SwiftUI
  
  
  /*
  
   Reproduction Steps:
   ===================
   
   
   #1.  Build on iPad mini (other devices will work in split screen, but Detail view must be showing, sidebar view must be hidden)
   #2.  Tap Back Button
  
   =====================
   Crash
   Thread 1: Simultaneous accesses to 0x7f8af986bae0, but modification requires exclusive access
   
   // Note: if any of the &quot;safe&quot; alternatives are used below, the bug won't surface.
   
   */
  
  // MARK: - Model
  
  public struct SafeSetting&lt;V&gt; {
      public let key: String
      public let defaultValue: V
      
      public init(name: String, defaultValue: V) {
          self.key =  name
          self.defaultValue = defaultValue
      }
  }
  
  public struct CrashSetting&lt;V&gt; {
      public let key: String
      public let defaultValue: V
      
      public init(namespace: String, name: String, defaultValue: V) {
          /// - NOTE:  I believe this is the main issue, but why?
          self.key = String(namespace + &quot;.&quot; + name)
          self.defaultValue = defaultValue
      }
  }
  
  enum Settings {
      static let safe = SafeSetting(name: &quot;safe&quot;, defaultValue: &quot;&quot;)
      static let crash = CrashSetting(namespace: &quot;com.mynamespace&quot;, name: &quot;crash&quot;, defaultValue: &quot;&quot;)
  }
  
  // MARK: - View
  
  @main
  struct NavigationLinkCrashApp: App {
      var body: some Scene {
          WindowGroup {
              RootView()
          }
      }
  }
  
  struct RootView: View {
      // MARK: Safe
      /*
      @AppStorage(Settings.safe.key)
      var safe = Settings.safe.defaultValue
      */
      
      
      // MARK: Crash
      @AppStorage(Settings.crash.key)
      var crash = Settings.crash.defaultValue
      
      @ViewBuilder var content: some View {
          NavigationView {
              Sidebar()
              Color.yellow
          }
      }
      var body: some View {
          content
      }
  }
  
  
  struct Sidebar: View {
      var body: some View {
          // List { NavigationLink } hierarchy is needed!
          List {
              NavigationLink {
                  Color.red
              } label: {
                  // MARK: Safe
                  /// `Text(&quot;&quot;)`
                  /// `Label(&quot;&quot;, systemImage: &quot;circle&quot;)`
                  /// `Color.green`
                  
                  // MARK: Crash
                  Text(&quot;crashes&quot;)
                  //Label(&quot;crashes&quot;, systemImage: &quot;circle&quot;)
              }
          }
          // List Style is needed!
          
          // MARK: Safe
          //.listStyle(SidebarListStyle())
          //.listStyle(InsetListStyle())
      
          
          // MARK: Crash
          .listStyle(InsetGroupedListStyle())
          //.listStyle(PlainListStyle())
          //.listStyle(GroupedListStyle())
      }
  }
  </code></pre>