Master data relationship lost after application update

I have a difficult problem that I cannot find the answer to. My data model is structured as follows:

Version 1:
the project has many places
location has many projects
But by mistake, the reverse between them was never set.

Version 2:
Same as above, but now inverted.

An example of my problem might be the following:
In version 1, I have two projects that have the same location. When I launch version 2 and my mapping model is being processed, the original project, in order to own the location, loses its connection with this location, and now the location is displayed only as part of one of the projects, and not both of them.

I understand that this problem is probably caused by the fact that I do not establish feedback between projects and locations, but is there anything I can do to keep the data stored in two versions of the application / data model?

Edit: I tried the proposed mapping model, and I tried to manually create the mapping model. I am currently using the NSMigratePersistentStoresAutomaticallyOption key when I created my NSPersistentStoreCoordinator .

In addition, to be clear, I have two versions of my data model, and the migration is successful, the only problem is that the relationship is not saved as intended.

Edit 2: I realized that I need subclasses of NSEntityMigrationPolicy. I do not want to do full user migration, I would prefer to keep the rest of my automatic migration, if possible. Does anyone know of any good tutorials or examples on a subclass of NSEntityMigrationPolicy that will be relevant to my purpose? I couldn’t find much, and as far as I can tell, there are very few links in the Yabloko documents.

Edit 3: I have not been able to understand for my whole life how to set up feedback using NSEntityMigrationPolicy. My problem is a little different now than I described earlier. Does anyone know any convincing example of how to do this?

+4
source share
4 answers

For everyone who faces this situation, there was actually a very simple answer that I did not consider.

I did not go too far with an attempt to subclass NSEntityMigrationPolicy - I could not find a good sample of code that went beyond the basics. In some situations, subclassing NSEntityMigrationPolicy might be the best solution. But if you are in the same situation as me - (bearing in mind that you have set up the correct fields for reverse relations in your model, but you forgot to specify countermeasures), I believe that I have a simpler solution.

Instead of subclassing NSEntityMigrationPolicy I did the following (which is actually much simpler than IMO):

(remember that my model is configured as follows: Projects ↔ Locations, has-and-of-to-many relationship)

  • I saved my old data model file (.xcdatamodel) in my application. When my application first loads, it loads my NSManagedObjectModel from the old data model file. Then I continued the cycle of all projects in my database and through all locations for each project, and for each location I would set the “project” field manually - if I set up my model correctly for the first time, it would be automatically completed using reverse relationship. This 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 NSManagedObjectContext

  • Then I got rid of my NSManagedObjectContext , NSManagedObjectModel , NSPersistentStoreCoordinator and rebuilt them using my new xcdatamodel file. The new model file contains inverse relationships and they are configured correctly. When transferring data from the SQLite database, all the data was saved, and the reverse relationships that I manually configured were still in place. When creating a new NSPersistentStoreCoordinator be sure to specify the NSMigratePersistentStoresAutomaticallyOption parameter.

0
source

Unsuccessful.

You will need to perform the manual migration by subclassing NSEntityMigrationPolicy: (

Read the documentation here - in particular, the section Object custom transfer policies.

You will need to create feedback yourself as part of the migration.

S

+3
source

Set up a persistent storage coordinator using the automatic migration model and see if this works. In addition, you created a new version of the model, right? Master data cannot be displayed without using both models.

 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 } 
+1
source

Is your second model randomly modified in a one-to-many relationship?

If so, your migration can only assign space to one project, because it will be converted from the old to the new.

(This is an assumption!)

+1
source

All Articles