Core Data migration failing on Release builds only - ios

I added an attribute to a Core Data model (iOS). I was on version 2, so I added a new model and saved the new attribute under version 3.
My loading code is standard (initializing a MOM using initWithContentsOfURL: pointing at my .momd directory inside my bundle), then passing that MOM to a NSPersistentStoreCoordinator.
Then, I download my app from the App Store, run it, and then try to test the migration. Lightweight migration is working fine on my Simulator and my phone in Debug (tethered to Xcode 4).
However, when I sync a Release build (using the "Archive" feature on Xcode) using iTunes, my app's data store fails to migrate with this error:
Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed.
(Cocoa error 134130.)", reason=Can't find model for source store}
Worse yet, the store gets lost in some voodoo state where it cannot escape - if I re-tether the device and install a Debug build from Xcode again, I can't "recover" the migration.
The error message above omits the details about my entities. I can provide that if helpful, but any ideas on what might be wrong here? I've read just about every question on SO about this error code and so far all of them have failed to produce a result.
I tried a manual migration, but it also yielded an error. However, since I only added 1 attribute to 1 entity, it doesn't make sense that lightweight migration isn't working.

Every time I've seen this problem, in my code or someone else, it has been XCode getting into some kind of bad state with regards to the model versions it deploys in the application. Open the Release IPA archive of your app, and look at what mom files are in it. If you see one of the previous versions missing, that is likely this same problem. As for a solution, I've had mixed results; some variation of the following steps works for me, but never in a consistent order.
Clean & delete the derived data for the project (make sure you have an archive of your released app saved so you can symbolicate if necessary)
Rename the previous model version files (in xcode), adding or removing the space between the name and the digit representing the version number (if it has a space, replace with '_', if it doesn't have a space try adding one)
Add a new version of the model, without changing any of it's content, and make it the 'current' version
I know these steps seem like grasping at straws, which is exactly what it feels like whenever this happens to me. You might consider using one of your support incidents with Apple, it would be nice to know what their official answer to this issue is.

Related

The model used to open the store is incompatible with the one used to create the store Xcode 7.1

I created a core data model that was working fine until I added a new entity to the model, my code is fine I think... because Xcode can't detect any errors, but as soon as I run my app on the simulator the compiler gives me an error saying
reason=The model used to open the store is incompatible with the one used to create the store
I don't quite understand what this error means. I did search for references regarding this error but the one I found is outdated it was from Xcode 3.2, I'm currently running Xcode 7.1.1, interestingly enough there's no check mark on none of the replies. Despite that I still tried to use some of those methods and nothing has worked.
Things I tried
Resetting iOS Simulator -> Reset Contents and Settings...
Deleted the [Simulator App Folder]/Document/*.sqlite and ran the project
Have you had the same problem? If so can you help me troubleshoot it? Thanks.
That error means that you changed the model so that it doesn't match the one you were using when you created the app's data. It's the classic sign of changing a data model and not either doing model migration or deleting the app from the phone/simulator (and it's been pretty much the same as long as Core Data has existed, so old information is not irrelevant). Deleting the app from the simulator would normally be all that's needed to run the app again. If it's not working, then you're somehow leaving old data in place.
The best way to delete an app from the simulator is usually the same as you'd delete an app on a real device-- click and hold until the app icon starts moving, then click the "x" on its icon to delete it.
If deleting the app doesn't help, you're using old data some other way. Maybe you have some pre-loaded data built into the app that was configured using an old version of the model?
My favorite solution is to open the sqlite database with a third party app, then open the Z_MetaData table and delete the row found in there.
Works like a charm every time.
It may affect your 'migration' if you do migrations.
We do not, so not an issue.

Compilation failed for data model at path after upgrading Xcode

After upgrading to Xcode 4.3.2 I have started getting the following error when i do a build:
Compilation failed for data model at path '/Users/Administrator/Library/Developer/Xcode/DerivedData/AppName-ffeggcadwojrwacrwosvadafamrk/Build/Products/Debug-iphoneos/Data.momd/Data.mom'
This was not happening before the upgrade and if i check out the same project on machine running the previous version of Xcode it works fine. No Errors.
I have found a couple of similar posts here and here but none of the answers have helped at all. From the error message the file path it gives doesn't contain a file so i'm guessing its a property somewhere that i need to change but i just can't find it.
Please help, thanks.
EDIT: On further investigation, i can stop it from erroring if i remove certain entities from the data model file, we are currently using a large pre created sqlite file to load data so i could recreate the data model file but that would mean recreating the sqlite which would be a bit of a pain. Anyone know why certain entities cause this?

Coredata inferred mapping model is failing on my old databases

I'm using CoreData for one of my databases, and I'm having trouble with an upgrade to my App.
My old databases are no longer loading ... I get the error "Cocoa error 134190" which I believe means that the inferred mapping model is failing.
When I started editing this version of my App, I created a new model version, and I simply added one new attribute to one entity, and two new attributes to another entity. Nothing was changed or deleted.
I've checked inside my App's directory, and all of the model versions are there.
I even have a snapshot of my previous version of the App, and with that snapshot I can load my core data files.
To debug what is going wrong, I took the exact data model from my previous snapshot and used it in my current version, and I still get the error.
I've even done a "diff" on every file in my ".app" directory packages, and there are only two files that are different between the two. One is the actual binary for the application, and the other is the "Info.plist" file. I looked at the Info.plist files, and they too are basically identical (the new one has a newer bundle version, but no other change).
Any idea why the inferred mapping is broken?
I am getting the same error even when the data models are completely unchanged from my last version which works.
This is extremely frustrating.
Any information as to how to track down problems like this in the future would also be greatly appreciated!!!
I found the answer ... the problem was that in my new version, I was rearranging the locations of my files. I did so by simply moving the database to a new location. What I didn't realize is that coredata databases have some knowledge of their path built in, and you can't simply move them.
I needed to change the location of the database using the NSPersistentStoreCoordinator's method:
migratePersistentStore:toURL:options:withType:error:
My fault, but I really wish that core data error messages weren't so cryptic.
In the next version of my App, I'm going to remove core data completely.
Ron
I don't believe core data actually cares where the database file is, but I could be wrong. In the times I have seen this error, specifically "Cocoa error 134190" while trying to infer a model mapping, it's been the case that there was actually a problem trying to migrate to the next version of the model.
For example, when I found this thread today because I was helping someone with this problem, the real underlying issue was that the type of one of the attributes was accidentally changed. The automatic migration can't handle that sort of change.
A somewhat easy way to look at the changes between two model versions is to diff the description files themselves on the command line with diff:
diff yourNameHere.xcdatamodeld/yourNameHere5.xcdatamodel/contents yourNameHere.xcdatamodeld/yourNameHere4.xcdatamodel/contents
(That assumes you have a core data model description named "yourNameHere" and you're looking at a problem migrating from version 4 to 5. You'll have to adjust for your specific files.) In this diff you should see whatever additional things you've added, but you're really looking for something like a type changing when it shouldn't.

Changed in XCode to mapping model aren't updated in simulator bundle

I created a mapping model to migrate between two core data models. I then edited my mapping model, (added a custom migration policy, changed a mapping name, etc.), ran the simulator, and... nothing changed. The old mapping model was loaded, ignoring my changes. Looking into the bundle that the simulator runs, the size of the .cdm file didn't change as well.
Another strange problem is that sometimes the the mapping model can't even be found, resulting in a migration error. In this case, the .cdm file in the bundle exists but is very small - less than 0.5k (my working file is ~14k).
I've tried running "Clean", emptying Xcode's DerivedData directory and resetting the simulator. They solve the problem only for new mapping models, but editing them will again not work. This makes it virtually impossible to work with mapping models. Very frustrating :-(
Doing a clean all (holding down the option key while selecting clean from the menu) will remove the derived data and will allow the changes in the mapping model to be picked up.
Out of habit, I always do a clean all when I am working with any files inside of Xcode that are not specifically source code files.
I would also suggest filing a radar. This has been an issue since mapping models came out in Leopard.
It is not an issue with the Simulator but an issue with Xcode not watching for those files to change. Once the compiled copies are removed (via the clean all), Xcode will copy over the entire app bundle to the sim again.
However, when you are testing a migration, I find it helpful to "reset" the sim anyway, usually bad to a copy that didn't have the mapping model or the new model that you are migrating to.

Using one xcdatamodelId in two app

I created one project with core data that will work with unchangeable database. And I don't want to write code in this project , that will programmatically populate this database. So, I create second project with core data, add existing xcdatamodel from first project without copying(only references). There i populate my database, open it with mozilla plug-in and it successfully filled. Then I copy ,my *.sqlite file and manually replace it with old file in first project. It causes error:"The model used to open the store is incompatible with the one used to create the store". But I use for both files the same xcdatamodelid. Where my error?
Sorry for my english, I really need help.
P.S. when I open sqlite file from first project and second (with commented code of populate base) in FileMerge - second is already empty. I appreciate any advice or help.
Karoly S nicely answers the question. I have a hint that I frequently employ that may prevent this out of sync situation. Instead of two Xcode projects trying to share one model file, just create one Xcode project with two targets. Each target will use the same model file, any class definitions derived from that model, and possibly other code. My second target is a Mac OS command line program that generates the database, while my first target continues to be the iOS app that reads that database. The Mac OS target will overwrite the database file in a project subdirectory, ensuring it's up-to-date. If I make any changes to the model, Xcode knows to update both targets.
Did you change your Model Definitions in any way? The error you are seeing is because there is some difference between the model from when you created it, and again from when you are trying to reference it. Are you running on the simulator? Try to delete both of your apps to clear the data related to it, as it might be out of date. Afterwards simply rebuild both of your projects as that will update your core data database.
EDIT: To clarify a bit more, your core data model is out of sync, this is generally caused by you building and running an app, and a database being created, then redefining your object model, this can be done in a variety of ways, most likely caused by the addition of an attribute or entity. So when you are trying to load the database there are fields that the app and core data are looking for, but are not there because they did not exist when the database was created. I hope this helps.

Resources