title: Swift - CoreData persisting after deleting until recompile
body: <p>I am trying to use CoreData to save my user data. However, the deleted data does not get updated, even when I save the context. Only upon another compile of the app will the data show correct.</p>
  <p>I have searched online for this answer, and multiple suggested putting <strong>appDelegate.saveContext()</strong>. However, even after this it does not work.</p>
  <p>Example: I am signed in as User 1. User 1 signs out. User 2 logs in. User 1's data is still being displayed.</p>
  <p>Delete:</p>
  <pre><code>func deleteCoreData(){
      let fetchRequest: NSFetchRequest&lt;NSFetchRequestResult&gt; = NSFetchRequest(entityName: &quot;User&quot;)
      let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
      
      guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
      let context = appDelegate.persistentContainer.viewContext
      
      do
        {
          try context.execute(deleteRequest)
          
        } catch let error as NSError {
          print(error)
        }
      appDelegate.saveContext()
  }
  </code></pre>
  <p>Read:</p>
  <pre><code>func retrieveUserData() -&gt; [String: String]{
      var arr = [String: String]()
      if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
          let context = appDelegate.persistentContainer.viewContext
          let fetch = NSFetchRequest&lt;User&gt;(entityName: &quot;User&quot;)
          do {
              let results = try context.fetch(fetch)
              
              print(&quot;Fetching: &quot;, results)
              for result in results {
                  arr[&quot;name&quot;] = result.name
                  arr[&quot;schoolName&quot;] = result.schoolName
                  arr[&quot;school&quot;] = result.school
                  arr[&quot;programCode&quot;] = result.programCode
                  arr[&quot;classOf&quot;] = result.classOf
                  arr[&quot;username&quot;] = result.userId
                  arr[&quot;programName&quot;] = result.programName
                  arr[&quot;username&quot;] = result.username
                  arr[&quot;userId&quot;] = result.userId
              }
          }
          catch {
              print(&quot;Cannot retrieve&quot;)
          }
      }
      return arr
  }
  </code></pre>
  <p>Create</p>
  <pre><code>func createNewCoreUser(login: String, data: [String: String]) {
      if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
          print(&quot;CoreData has been created&quot;)
          let context = appDelegate.persistentContainer.viewContext
          guard let entityDescription = NSEntityDescription.entity(forEntityName: &quot;User&quot;, in: context) else { return }
          let newValue = NSManagedObject(entity: entityDescription, insertInto: context)
          newValue.setValue(login, forKey: &quot;userId&quot;)
          newValue.setValue(data[&quot;name&quot;], forKey: &quot;name&quot;)
          newValue.setValue(data[&quot;classOf&quot;], forKey: &quot;classOf&quot;)
          newValue.setValue(data[&quot;programCode&quot;], forKey: &quot;programCode&quot;)
          newValue.setValue(data[&quot;school&quot;], forKey: &quot;school&quot;)
          newValue.setValue(data[&quot;schoolName&quot;], forKey: &quot;schoolName&quot;)
          newValue.setValue(data[&quot;programName&quot;], forKey: &quot;programName&quot;)
          newValue.setValue(data[&quot;username&quot;], forKey: &quot;username&quot;)
          do {
              try context.save()
              
              (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(mainTabBarController)
              
          } catch {
              print(&quot;Storing data Failed&quot;)
          }
      }
  }
  </code></pre>