title: SwiftUI & Unit test fails: Can't add the same store twice when using core data stack
body: <p>After updating Xcode to 13.4 my core data persistence stack started to fail for SwiftUI previews and Xcode Unit tests.</p>
  <h2>Error message:</h2>
  <pre><code>Unresolved error Error Domain=NSCocoaErrorDomain Code=134081 &quot;(null)&quot; UserInfo={NSUnderlyingException=Can't add the same store twice}, [&quot;NSUnderlyingException&quot;: Can't add the same store twice ...
  </code></pre>
  <h2>Persistence stack:</h2>
  <pre><code>struct PersistenceController {
      static let shared = PersistenceController()
  
      let container: NSPersistentContainer
  
      init(inMemory: Bool = false) {
          container = NSPersistentContainer(name: &quot;Persistence&quot;)
          if inMemory {
              container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: &quot;/dev/null&quot;)
          }
          let description = NSPersistentStoreDescription()
  
          description.shouldInferMappingModelAutomatically = true
          description.shouldMigrateStoreAutomatically = true
  
          container.persistentStoreDescriptions.append(description)
          container.loadPersistentStores(completionHandler: { (storeDescription, error) in
              if let error = error as NSError? {
                  fatalError(&quot;Unresolved error \(error), \(error.userInfo)&quot;)
              }
          })
          container.viewContext.automaticallyMergesChangesFromParent = true
      }
  }
  </code></pre>
  <h2>Usage for SwiftUI previews:</h2>
  <pre><code>struct MyScreen_Previews: PreviewProvider {
      static var previews: some View {
          MyScreen()
              .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
      }
  }
  </code></pre>
  <h2>Extension to support SwiftUI preview:</h2>
  <pre><code>extension PersistenceController {
      static var preview: PersistenceController = {
          let result = PersistenceController(inMemory: true)
          // Pre-fill core data with required information for preview.
      }
  }
  </code></pre>