title: Crashes during CoreData fetching on serial queue
body: <p>I went through many discutions and subjects about CoreData, but I keep getting the same problem.</p>
  
  <p>Here's the context : I have an application which have to do several access to CoreData. I decided, for simplifying, to declare a <strong>serial</strong> thread specifically for access (queue.sync for fetching, queue.async for saving). I have a structure that is nested three times, and for recreating the entire structure, I fetch subSubObject, then SubObject and finally Object</p>
  
  <p>But <strong>sometimes</strong> (like 1/5000 recreation of "Object") CoreData crash on fetching results, with no stack trace, with no crash log, only a
  <strong>EXC_BAD_ACCESS (code 1)</strong></p>
  
  <p>Objects are not in cause, and the crash is weird because all access are done in the <strong>same thread</strong> which is a <strong>serial thread</strong></p>
  
  <p>If anyone can help me, I will be very grateful !</p>
  
  <p>Here's the structure of the code :</p>
  
  <pre class="lang-cs prettyprint-override"><code>private let delegate:AppDelegate
  private let context:NSManagedObjectContext
  private let queue:DispatchQueue
  
  override init() {
      self.delegate = (UIApplication.shared.delegate as! AppDelegate)
      self.context = self.delegate.persistentContainer.viewContext
      self.queue = DispatchQueue(label: "aLabel", qos: DispatchQoS.utility)
      super.init()
  }
  
  
  (...)
  
  public func loadObject(withID ID: Int)-&gt;Object? {
  
      var object:Object? = nil
  
      self.queue.sync {
  
          let fetchRequest = NSFetchRequest&lt;NSManagedObject&gt;(entityName: "Name")
          fetchRequest.predicate = NSPredicate(format: "id == %@", NSNumber(value: ID))
  
          do {
              var data:[NSManagedObject]
  
              // CRASH HERE ########################
              try data = context.fetch(fetchRequest)
              // ###################################
  
              if (data.first != nil) {
                  let subObjects:[Object] = loadSubObjects(forID: ID)
                  // Task creating "object"
              }
          } catch let error as NSError {
              print("CoreData : \(error), \(error.userInfo)")
          }
      }
  
      return object
  }
  
  private func loadSubObjects(forID ID: Int)-&gt;[Object] {
  
      var objects:[Object] = nil
  
      self.queue.sync {
  
          let fetchRequest = NSFetchRequest&lt;NSManagedObject&gt;(entityName: "Name")
          fetchRequest.predicate = NSPredicate(format: "id == %@", NSNumber(value: ID))
  
          do {
              var data:[NSManagedObject]
  
              // OR HERE ###########################
              try data = context.fetch(fetchRequest)
              // ###################################
  
              if (data.first != nil) {
                  let subSubObjects:[Object] = loadSubObjects(forID: ID)
                  // Task creating "objects"
              }
          } catch let error as NSError {
              print("CoreData : \(error), \(error.userInfo)")
          }
      }
  
      return objects
  }
  
  (etc...)
  </code></pre>