Core Data Appears to Lose Data After Xcode Upgrade

Is an app's core data database deleted when an app is updated from a App Store?

That depends entirely on you. When you set up a Core Data stack, you can point your NSPersistentStoreCoordinator at a particular file anywhere in your app's writable folders that you want. Where you place that file determines whether it gets migrated during an app update.

A common choice is to stick your database file in the user's Documents directory, which will cause iOS to copy it along when installing an update to your application. Then, on launch, you're responsible for dealing with that database as you see fit (updating data in it, migrating your schema, etc.). Placing the file elsewhere - inside a temporary directory, for example - may cause it to be lost during an update.

See the File System Programming Guide and Core Data Model Versioning and Data Migration for more information.

Coredata Lightweight migration lost data

NSPersistentContainer enables Lightweight Migration by default. Delete the extra code.

And the migration doesn't delete any data. If the migration is not possible you get an error.

Core Data sometimes loses data

There are three possible causes.

Write Conflicts

Core data generally wants writes to be done in a single synchronous way. If you write in multiple ways at the same time to the same object (even if they are touching different properties and don't strictly conflict), it will be a merge conflict. You can set a merge policy (by default the value is 'error' - meaning don't apply the changes) but that is really a bad solution because you are tell core-data to lose information silently. see NSPersistentContainer concurrency for saving to core data for a setup to prevent merge conflicts.

Closing the app with unsaved data

If you setup your core-data correctly this shouldn't happen. The correct way to setup core data to only read from the 'viewContext' and write in a single synchronous way. Each writing is done in a single atomic block and the UI is only updated after it is saved. If you are displaying information from a context that is not saved to disk this can be a problem. For example it appears that your app only uses a single main-thread context for both reading and writing. Making changes to that context and not calling save will leave the app in a state where there are major changes that are only in memory and not on disk.

There is an error saving to disk

This is by far the rarest event, but there are users that have really really full disks. If this happens there is NOTHING that you can do to save. There is physically no room left on the disk. Generally the correct thing to do is to tell the user and leave it at that.


Without knowing more about your particular setup it hard to say for certain what your problem is. I would recommend the following setup:

  1. use NSPersistentContainer
  2. only read from the viewContext and never write to it.
  3. make an operation queue for writing to core data
  4. for every operation, create a context, make changes to it, and then save. Do not pass any managed object into or out of these blocks.

This should deal with all but the third problem.

Bundle Identifier Changed Core Data Lost

This took me a while to figure out but here is how I fixed the issue.

  1. Change your product name under Build Settings -> Packaging -> Product Name to the desired product name. This will change you Bundle Identifier to the desired name.

Sample Image

Sample Image


  1. Clean your project by selecting Product -> Clean.

  2. Select your CoreData model in the project navigator and then select the "Data Model Inspector" in the Inspector tab. Change the project name under class to the new class name. I got stuck here since I did not know Spaces, dashes, . are all replaced by underscores. You can see that my Product Name is FLO-Cycling and the CoreData class is FLO_Cycling.

Sample Image

Sample Image


  1. If you have imported the Swift bridging header file anywhere make sure to update it.

Sample Image

I hope this can help someone.

Take care,

Jon

Xcode Core Data models are missing

It's hard to say exactly what happened, but something is causing your files to disappear. It's not in any way related to Core Data or Swift playgrounds-- you've got a serious problem somewhere and it's making you lose data.

These files should be in your git repository. If you can't find them by undoing changes since your last commit, you need to dig deeper. If they were ever committed, they're recoverable, but going back to the last commit may not be enough. They should also exist in whatever system you use to back up your hard drive.

Core Data may be able to help you recover the models by other means. If you have an older compiled copy of the app somewhere, it will contain compiled versions of the data model files. Xcode can import those and re-create the old model files. You'd do this by creating a new model file, then going to the "Edit" menu and selecting "Import...".

But, all this is secondary to your real problem, which is that you're losing files and you don't know why. You could lose more files in the future. Whatever happened could happen again. You need to figure out why you're losing files and fix that problem.



Related Topics



Leave a reply



Submit