I have a tough issue that I cannot find an answer to. My data model is structured like this:
Version 1:
project has many locations location has many projects But by mistake the inverse between the two was never set.Version 2:
The same as above, but the inverse is now setup.An example of my problem would be as follows:
In version 1, I have two projects that own the same location. When I launch version 2 and my mapping model is processed, the original project to own the location loses it's relationship to that location and now the location only shows up as part of one of the projects instead of both of them.I recognize that this issue is likely caused by me not setting up the inverse relationship between projects and locations, but is there anything that I can do to make data persist across the two versions of the app/data model?
Edit:
I have tried an inferred mapping model and I have tried creating a mapping model manually. I am currently only using the NSMigratePersistentStoresAutomaticallyOption
key when I created my NSPersistentStoreCoordinator
.
Also, just to be clear, I have two versions of my data model and the migration is successfully occurring, the only problem is that the relationships are not persisting as intended.
Edit 2: I have figured out that I will need to subclass NSEntityMigrationPolicy. I do not want to do an entirely custom migration, I would prefer to keep the rest of my migration automatic if possible. Does anyone know of any good tutorials or examples on subclassing NSEntityMigrationPolicy that would be relevant to my purpose? I haven't been able to find much and as far as I can tell there is extremely little reference to it in Apples docs.
Edit 3: I ca开发者_JS百科nnot for the life of me figure out how to setup an inverse relationship using NSEntityMigrationPolicy. My problem is a bit different now than I described earlier. Does anyone know of any solid example on how to do this?
Unlucky.
You will have to do the migration manually by subclassing NSEntityMigrationPolicy :(
Take a read of the documentation here - specifically the section on Custom Entity Migration Policies.
You will have to create the inverse relationship yourself as part of the migration.
S
Set up your persistent store coordinator using an auto-migration model, and see if that works. Also, you did create a new model version, right? Core Data cannot map without both models in place.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
}
Is your second model accidentally changed the relationship from 'one-to-many'?
If so, your migration might only assign a location it to one project as it converts from the old to new model.
(This is a guess!)
For anyone else that comes across this situation, there was actually a very simple answer that I had not considered.
I did not get too far with attempting to subclass NSEntityMigrationPolicy
-- I couldn't find any good sample code that went beyond the basics. In certain situations, subclassing NSEntityMigrationPolicy
may be the best solution. But if you are in the same situation as me -- (meaning you have setup the correct fields for inverse relationships in your model but you forgot to actually specify the inverses), I believe I have a simpler solution.
Instead of subclassing NSEntityMigrationPolicy
, I did the following (which is actually much simpler IMO):
(keep in mind that my model is setup as follows: Projects <--> Locations, a has-and-belongs-to-many relationship)
I kept my old data model (.xcdatamodel) file in my app. When my app first loads, it loads my
NSManagedObjectModel
from the old data model file. I then proceeded to loop through all of the projects in my database, and through all of the locations for each project, and for each location I would set the "project" field manually -- if I had set up my model correct the first time, this would have been completed automatically using inverse relationships. Here is what my code looks like:NSArray *projects = [context executeFetchRequest:request error:&error]; for(Project *project in projects) { for(Location *location in project.locations) { // set the inverse relationship manually [location addProjectsObject:project]; } }
I saved my
NSManagedObjectContext
.I then got rid of my
NSManagedObjectContext
,NSManagedObjectModel
,NSPersistentStoreCoordinator
and rebuilt them using my new xcdatamodel file. The new model file contains the inverse relationships and they are setup correctly. When migrating the data from the SQLite database, all of the data persisted and the inverse relationships that I setup manually were still in place. When creating the newNSPersistentStoreCoordinator
, make sure to specify theNSMigratePersistentStoresAutomaticallyOption
option.
精彩评论