title: Restore Core Data
body: <p>I've managed to create a local backup of my PersistentStore using the following code:</p>
  
  <pre><code>func backup(){
      let backUpFolderUrl = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first!
      let backupUrl = backUpFolderUrl.appendingPathComponent("backup")
      let container = NSPersistentContainer(name: "MyApp")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in })
  
      let store:NSPersistentStore
      store = container.persistentStoreCoordinator.persistentStores.last!
      do {
          try container.persistentStoreCoordinator.migratePersistentStore(store,
                                                                          to: backupUrl,
                                                                          options: nil,
                                                                          withType: NSSQLiteStoreType)
      } catch {
          print("Failed to migrate")
      }
  }
  </code></pre>
  
  <p>This seems to work. The backup files are created. This answer:
  <a href="https://stackoverflow.com/questions/48613271/backup-core-data-locally-and-restore-from-backup-swift">Backup core data locally, and restore from backup - Swift</a>
  Suggests I need to deallocate my NSPersistentContainer and then use replacePersistentStore(at:destinationOptions:withPersistentStoreFrom:sourceOptions:ofType:) to restore my backup. So I tried like this:</p>
  
  <pre><code>func restoreFromStore(){
      let storeFolderUrl = FileManager.default.urls(for: .applicationSupportDirectory, in:.userDomainMask).first!
      let storeUrl = storeFolderUrl.appendingPathComponent("MyApp")
      let backUpFolderUrl = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first!
      let backupUrl = backUpFolderUrl.appendingPathComponent("backup")
  
      let container = NSPersistentContainer(name: "MyApp")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in })
  
      let stores = container.persistentStoreCoordinator.persistentStores
  
      for store in stores {
          do {
              try container.deall
          } catch {
              print("Couldnt clear store")
          }
      }
  
      do{
          try
              container.persistentStoreCoordinator.replacePersistentStore(at: storeUrl,
                                                                          destinationOptions: nil,
                                                                          withPersistentStoreFrom: backupUrl,
                                                                          sourceOptions: nil,
                                                                          ofType: NSSQLiteStoreType)
      } catch {
          print("Failed to restore")
      }
  }
  </code></pre>
  
  <p>This doesn’t seem to do anything. I don’t get any errors or warnings but it also doesn’t seem to restore my core data at the point I backed it up.</p>
  
  <p>Sorry, I know this is probably awful code. I actually achieved the result I wanted using entirely FileManager but it seemed to make my console really angry. I wanted to do it properly, without any warnings. </p>