title: Core Data is not persisting even after context.save()
body: <p>I am saving different activities to Core Data. While the app is working i can easily retrieve this data. But after the app I closed the data is gone even if even after I explicitly save it using <code>context.save()</code>. Any ideas why this happens?</p>
  <pre><code>private func getContext() -&gt; NSManagedObjectContext {
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      return appDelegate.persistentContainer.viewContext
  }
  
  func retrieveTestEntity() -&gt; TestEntity? {
      let managedContext = getContext()
      let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: &quot;TestEntity&quot;)
      
      do {
          let result = try managedContext.fetch(fetchRequest) as! [TestEntity]
          
          if result.count &gt; 0 {
              // Assuming there will only ever be one Entity in the app.
              return result[0]
          } else {
              return nil
          }
      } catch let error as NSError {
          print(&quot;Retrieving user failed. \(error): \(error.userInfo)&quot;)
          return nil
      }
  }
  
  func saveActivity(_ activity: Activity) {
      let managedContext = getContext()
      guard let testEntity = retrieveTestEntity() else { return }
      
      testEntity.activity.append(activity)
      
      do {
          print(&quot;Saving session...&quot;)
          try managedContext.save()
      } catch let error as NSError {
          print(&quot;Failed to save session data! \(error): \(error.userInfo)&quot;)
      }
  }
  </code></pre>