Search code examples
iphoneobjective-cioscore-datacore-data-migration

Core Data Relationship Lost After App Upgrade


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 cannot 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?


Solution

  • 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)

    1. 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];
          }
      }
      
    2. I saved my NSManagedObjectContext.

    3. 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 new NSPersistentStoreCoordinator, make sure to specify the NSMigratePersistentStoresAutomaticallyOption option.