title: (Swift) EXC_BAD_ACCESS in BlueTooth Messaging App
body: <p>I've continued to get EXC_BAD_ACCESS in my BlueTooth Messaging App. I'm using CoreData and have attempted to use a background context to manage multi-threading per <a href="https://duncsand.medium.com/threading-43a9081284e5" rel="nofollow noreferrer">https://duncsand.medium.com/threading-43a9081284e5</a>.</p>
  <p>Here's how my objects are set up:</p>
  <pre><code>extension PublicKey {
  
  @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;PublicKey&gt; {
      return NSFetchRequest&lt;PublicKey&gt;(entityName: &quot;PublicKey&quot;)
  }
  
  @NSManaged public var keyBody: Data?
  @NSManaged public var contact: Contact?
  @NSManaged public var encryptedSender: Encrypted?
  @NSManaged public var identity: Identity?}
  
  extension Contact {
  
  @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;Contact&gt; {
      return NSFetchRequest&lt;Contact&gt;(entityName: &quot;Contact&quot;)
  }
  
  @NSManaged public var id: UUID?
  @NSManaged public var nickname: String?
  @NSManaged public var timeLatest: Date?
  @NSManaged public var identity: Identity?
  @NSManaged public var messages: NSSet?
  @NSManaged public var theirKey: PublicKey?}
  </code></pre>
  <p>Where both Contact and PublicKey have an additional file with:</p>
  <pre><code>@objc(Contact) public class Contact: NSManagedObject {}
  </code></pre>
  <p>I'm declaring a background context upon each new BlueTooth thread with the code:</p>
  <pre><code>lazy var backgroundContext: NSManagedObjectContext = {
          let newbackgroundContext = PersistenceController.shared.container.newBackgroundContext()
          newbackgroundContext.automaticallyMergesChangesFromParent = true
          return newbackgroundContext
      }()
  </code></pre>
  <p>And I then pass that backgroundContext down into all of the BlueTooth messaging functions (which are called inside of a performAndWait block) and use it when creating objects, such as:</p>
  <pre><code>    let newEncrypted = Encrypted(context: backgroundContext)
      newEncrypted.receiverId = receiverId
      newEncrypted.senderId = senderId
      newEncrypted.messageType = messageType
      newEncrypted.timeCreated = timeCreated
      var pubKey = PublicKey.createPublicKey(key: senderKey!, backgroundContext: backgroundContext)
      do{
          try backgroundContext.save()
      }
      catch{
          fatalError(&quot;Unable to save background context: \(error)&quot;)
      }
      
      newEncrypted.senderKey = pubKey
  </code></pre>
  <p>The code always errors out with an EXC_BAD_ACCESS Code 1 error on the last line of this previous block; it seems to error out any time I assign an object to a field of a recently created object.</p>
  <p>If relevant, here's the createPublicKey method used:</p>
  <pre><code>static func createPublicKey(key: String, backgroundContext: NSManagedObjectContext) -&gt; PublicKey {
      let newKey = PublicKey(context: backgroundContext)
      newKey.keyBody = Data(base64Encoded: key, options: .ignoreUnknownCharacters)
      return newKey
  }
  </code></pre>