CoreData migration in iOS 7
Asked Answered
S

1

3

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

source code

Everything works fine in iOS6 but in iOS7 app crash

Spencerspencerian answered 4/5, 2014 at 19:31 Comment(2)
Crashes with what error message(s) and/or symptom(s), and at which part of the migration process? Saying it crashes is completely useless without some detail.Velocipede
firstly thanks for your reply so it crash when am trying to add my persistent store to my persistent store coordinator with error CoreData: error: failure reading metadata plist with data bytes: <30>Spencerspencerian
V
3

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.

Velocipede answered 6/5, 2014 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.