Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way To Fix Core Data Migration Error

I have an application which uses core data and I have set up lightweight migration. When you first create the app xcode generates default methods. The persistentStoreCoordinator getter calls the abort() method and it says it should be replaced in a shipping application. I think the best way for my app to handle this failed migration would be to completely reset core data and then reload all the info. Is this the best method and if so how would I go about doing this?

like image 782
carloabelli Avatar asked Jan 12 '23 14:01

carloabelli


1 Answers

The "best" answer always depends on your application.

  1. This error should only ever happen during development. With proper testing, you should never see this error in production. I always consider it a development level error and therefore a NSLog and abort() are appropriate.

  2. If your data is replaceable (i.e. you are using CD to cache data from the internet, etc.) then yes it makes sense to delete the database on this error and rebuild it from scratch. If your data is not replaceable then this is a terrible solution, see point 1.

Update 1

Putting this in an update because it is important and I don't want it getting lost in the comments:

@MarcusS.Zarra I know that this error should never happen, however I don't think it is unrecoverable.

In production, if you error on -addPersistentStore... it is unrecoverable as far as that database otherwise you would not have hit the error in the first place. Hence the two suggestions above, test more or replace the data.

There are two errors (ignoring iCloud) that you will get from that method, failed migration or failed to open the file.

If you failed to open the file the file is dead and is not going to be opened without human intervention.

If you failed the migration then you are missing the source data model and you are not going to recover it without a new build of the application that includes the source model.

Having a bad data file is extraordinarily rare. It indicates a fault with the file system or some other extremely unusual circumstance.

Not having the source data model is a 100% developer avoidable situation.

In either case, in production, you are not recovering from the error.

like image 129
Marcus S. Zarra Avatar answered Jan 19 '23 10:01

Marcus S. Zarra