Core Data migration failure between NSPersistenceF

2019-04-16 04:29发布

I have what appears to be a bug with Core Data in OS X 10.7.1.

My data store (NSSQLiteStoreType) is not being automatically migrated correctly.

As a side note the exact same Core Data models are used by an iOS version of the app.

-> Here is what appears to be happening.

Up until the latest release everything has been working fine --

I have 14 revisions of the model -- model 13 has been shipping for quite some time without incident.

Recently I updated my app and added a new model so I am at 14 (this is the version now used in the shipping apps).

The iOS version works just fine when migrating from model 13 to 14 -- so no need to worry about that.

So rather then talking about OS X versions lets use NSPersistenceFrameworkVersions

NSPersistenceFrameworkVersions 251 being 10.6.8 and NSPersistenceFrameworkVersions 358 being 10.7.1

If we use like versions -- meaning model 13 version 251 and migrate to model 14 version 251 it works fine.

same is true if we use model 13 version 358 and migrate to model 14 version 358 it also works fine

Here is where it gets interesting

if we migrate model 13 version 358 to model 14 version 251 it still works fine

However migrating from model 13 version 251 to model 14 version 358 does not work

CoreData: error: (1) I/O error for database at blah. SQLite error code:1, 'no such column: FOK_REFLEXIVE'

Again this only happens when going from 10.6.8 (NSPersistenceFrameworkVersions 251) to 10.7.1 (NSPersistenceFrameworkVersions 358) all other permutations work just fine.

Here are the options used for migration that get passed to addPersistentStoreWithType.

// Allow inferred migration from the original version of the application.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

One final thing, it appears if we only add attributes to model 14 the conversion will not break. It is only with the addition of an entity that will cause it to break.

Anybody else seeing this sort of problem. -thanks in advance.

2条回答
唯我独甜
2楼-- · 2019-04-16 05:01

This bug appears to have been fixed in iOS 5.1 and OSX 10.7.3

查看更多
Anthone
3楼-- · 2019-04-16 05:23

To clarify, use the unix sqlite3 tool with the path to the database file as a parameter. You can get your app's db file by pulling it from the organizer's device pane under Applications for a device... You'll get a package.

type ".headers on" for table headers

Find tables (.tables command) for entities with references between objects

Where Z_1REFERENCEDSETOFOBJECTS is the name of your table.

select * from Z_1REFERENCEDSETOFOBJECTS limit 2

to find the names of the table's headers; then run the following for the tables that have the REFLEXIVE column:

ALTER TABLE Z_1REFERENCEDSETOFOBJECTS ADD FOK_REFLEXIVE integer

Push it back down.

查看更多
登录 后发表回答