title: Error when working with NSManagedObjectContext in background thread
body: <p>So I keep getting this error when I save to core data.</p>
  
  <p><strong>Coredata[21468:13173906] [error] error: SQLCore dispatchRequest: exception handling request:  , ** -_referenceData64 only defined for abstract class.  Define -[NSTemporaryObjectID_default _referenceData64]! with userInfo of (null)</strong></p>
  
  <p>I recently finished a course on core data, the instructor brush a bit on the topic that core data isn't thread safe. So what he suggested is a child/parent context, so that is what I tried doing but kept getting the error from above. This is how my code looks.</p>
  
  <pre><code>struct Service {
  static let shared = Service()
  
  func downloadPokemonsFromServer(completion: @escaping ()-&gt;()) {
      let urlString = "https://pokeapi.co/api/v2/pokemon?limit=9"
      guard let url = URL(string: urlString) else { return }
  
      URLSession.shared.dataTask(with: url) { (data, response, error) in
          if let err = error {
              print("Unable to fetch pokemon", err)
          }
  
          guard let data = data else { return }
          let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
          privateContext.parent = CoreDataManager.shared.persistentContainer.viewContext
  
          let decoder = JSONDecoder()
          decoder.keyDecodingStrategy = .convertFromSnakeCase
  
          do {
              let pokemonJSON = try decoder.decode(PokemonsJSON.self, from: data)
              pokemonJSON.pokemons.forEach { (JSONPokemon) in
                  //Works fine with privateContext here
                 let pokemon = Pokemon(context: privateContext)
                  pokemon.name = JSONPokemon.name
                  pokemon.url = JSONPokemon.detailUrl
              }
  
              //Why does the privateContext work here
              //and doesn't crash my app.
              try privateContext.save()
              try privateContext.parent?.save()
              completion()
          } catch let err {
              print("Unable to decode PokemonJSON. Error: ",err)
              completion()
          }
      }.resume()
  }
  
  func fetchMoreDetails(pokemon: Pokemon, urlString: String, context: NSManagedObjectContext, completion: @escaping ()-&gt;()) {
      guard let url = URL(string: urlString) else { return }
      URLSession.shared.dataTask(with: url) { (data, response, error) in
          if let err = error {
              print("Unable to get more details for pokemon", err)
          }
  
          guard let data = data else { return }
          let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
          privateContext.parent = CoreDataManager.shared.persistentContainer.viewContext
  
          let decoder = JSONDecoder()
          decoder.keyDecodingStrategy = .convertFromSnakeCase
  
          do {
              let pokemonDetailJSON = try decoder.decode(PokemonDetailJSON.self, from: data)
              pokemonDetailJSON.types.forEach { (nestedType) in
                  // I can just change this to context and it works
                  // Why doesnt it work with privateContext and crashes my app
  
                  let type = Type(context: privateContext)
                  type.name = nestedType.type.name
  
                  pokemon.addToTypes(type)
              }
  
              try privateContext.save()
              try privateContext.parent?.save()
  
              completion()
          } catch let err {
              print("Unable to decode pokemon more details", err)
              completion()
          }
  
      }.resume()
  }
  }
  </code></pre>
  
  <p>So in the downloadPokemonsFromSever, I make a call to an api that parse the json and saves it into Coredata. I followed my instructor instructions, by creating a privateContext and then setting its parent to my mainContext. Then I create a new Pokemon that has a name &amp; url using my privateContext and NOT my mainContext. When I completely parse my Pokemon I go into another api that has more details on that Pokemon.</p>
  
  <p>This is where my app starts to crash. As you can see from the fetchMoreDetails there is a parameter that is context. When I try to create a new Type with privateContext it crashes my app. When I use the context that is passed through it works fine. I would like to know why privateContext works inside downloadPokemonFromServer and not in fetchMoreDetails. I left a comment above the line that I think that crashes my app. This is how I call it in my ViewController, using this action.</p>
  
  <pre><code>@objc func handleRefresh() {
  
      Service.shared.downloadPokemonsFromServer {
          let context = CoreDataManager.shared.persistentContainer.viewContext
          self.pokemonController.fetchedObjects?.forEach({ (pokemon) in
  
              Service.shared.fetchMoreDetails(pokemon: pokemon, urlString: pokemon.url ?? "", context: context) {
  
              }
          })
      }
      tableView.refreshControl?.endRefreshing()
  }
  </code></pre>