title: SwiftUI Coredata MacOS App extremely slow after a bit of use
body: <p>I'm writing a SwiftUI app that frequently saves and retrieves data from Coredata. After using my app for a few minutes, or doing something that causes lots of Coredata saves, the app becomes extremely slow and never speeds back up. I don't really even know where to start in troubleshooting this...</p>
  <p>This is my PersistenceController:</p>
  <pre><code>import CoreData
  
  struct PersistenceController {
      static let shared = PersistenceController(inMemory: false)
  
      let container: NSPersistentContainer
  
      init(inMemory: Bool = false) {
          container = NSPersistentContainer(name: &quot;Cubing_TimerApp&quot;)
          let storeURL = try! FileManager
                  .default
                  .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                  .appendingPathComponent(&quot;Cubing_TimerApp.sqlite&quot;)
          let storeDescription = NSPersistentStoreDescription(url: storeURL)
          if inMemory {
              container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: &quot;/dev/null&quot;)
          } else {
              container.persistentStoreDescriptions = [storeDescription]
          }
          container.loadPersistentStores(completionHandler: { (storeDescription, error) in
              if let error = error as NSError? {
                  fatalError(&quot;Unresolved error \(error), \(error.userInfo)&quot;)
              }
          })
          
      }
  }
  </code></pre>
  <p>This is an example of a function that might be saving to Coredata a lot:</p>
  <pre><code>func deleteSolve(_ solve: Solve) {
      viewContext.delete(solve)
      do {
          try viewContext.save()
      } catch {
          let nsError = error as NSError
          fatalError(&quot;Unresolved error \(nsError), \(nsError.userInfo)&quot;)
      }
  }
  </code></pre>
  <p>I have 2 Coredata entities, Instance and Solve. Instance has a one-to-many relationship with solve.</p>
  <p>I also have code that allows me to detect when the space key is pressed and released, which I figured might cause some slowness:</p>
  <pre><code>class KeyDetector: ObservableObject {
      @Published var spaceIsPressed: Bool = false
  }
  
  class KeyboardManager {
      static var isInitialized = false
      
      private static let keyDetector = KeyDetector()
      
      static var keyStates: [CGKeyCode: Bool] = [
          .kVK_Space: false,
      ]
      
      static public var initializer: KeyDetector {
          if isInitialized == false {
              pollKeyStates()
              isInitialized = true
          }
          
          return keyDetector
      }
      
      static func dispatchKeyDown(_ key: CGKeyCode)
      {
          if key == .kVK_Space {
              keyDetector.spaceIsPressed = true
          }
      }
  
      static func dispatchKeyUp(_ key: CGKeyCode) {
          if key == .kVK_Space {
              keyDetector.spaceIsPressed = false
          }
      }
      
      static func pollKeyStates() {
          for (code, wasPressed) in keyStates {
              if code.isPressed {
                  if !wasPressed {
                      dispatchKeyDown(code)
                      keyStates[code] = true
                  }
              } else if wasPressed {
                  dispatchKeyUp(code)
                  keyStates[code] = false
              }
          }
          
          DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(25)) {
              pollKeyStates()
          }
      }
  }
  </code></pre>
  <p>However, I tried disabling it and it was just about as slow.</p>
  <p>Another thing that seems like it is causing some lag is a list of Coredata objects:</p>
  <pre><code>struct SolveList: View {
      @State var instance: Instance
  
      var body: some View {
          List {
              ForEach(instance.solveArray.reversed()) { solve in
                  Text(String(solve.time))
              }
          }
          .id(UUID())
      }
  }
  </code></pre>
  <p>Ideally it should be able to show hundreds/thousands of items, and it seemed like it was causing some more lag with about 100. However the app is still very slow when there are only a couple.</p>
  <p>Unsurprisingly, CPU usage is pretty consistently at about 100%, almost all of which is from the main thread. I included the code which I thought was most relevant to the problem but I'm happy to share any of it in case what I put here isn't enough.</p>
  <p>Anyways, any help figuring out what's going on or how to speed it up would be very appreciated, since I'm kind of at a standstill with progress on it now.</p>
  <p>Thanks!</p>