CoreData migration in iOS 7

2019-04-13 12:01发布

Finally got this migration working, it was a big pain.

source code

Everything works fine in iOS6 but in iOS7 app crash

1条回答
来,给爷笑一个
2楼-- · 2019-04-13 12:22

Based on your comment and a bug report at http://openradar.io/15555487 I'm guessing that the full message is something like:

CoreData: error: failure reading metadata plist with data bytes: <33>
Unable to open database. Error: The file couldn’t be opened because it isn’t in the correct format.
Info: {
    NSUnderlyingException = "An error <null> occurred converting the metadata plist data: <33>";
}

Also based on that report, plus your source code: you're almost certainly leaving old wal and shm files around after the migration, which are causing problems after the migration. Beginning with iOS 7, Core Data uses WAL-mode journaling. That means there are a couple of extra files you need to worry about when copying or removing a persistent store. If your store file name is foo.sqlite, there's also a foo.sqlite-wal and foo.sqlite-shm in the same directory.

Your code removes the old SQLite file and replaces it with the new one, but leaves the existing journal files in place. That causes inconsistent data, since the journal files don't correspond to the main SQLite file any more. SQLite and Core Data can't make sense of what's left, so the app crashes.

The solution is most likely to make sure to clean up the extra files. When you make this call:

success = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:error];

...also remove the two extra files.

For future work, it's useful to put the persistent store file in its own directory with no other files. Then if you ever need to remove the store, you can just remove the directory in one step, with no need to care about what other extra files SQLite or Core Data might have created.

查看更多
登录 后发表回答