views:

121

answers:

2

I have a deployed app that samples measurements from sensors (e.g., Temp °C, Pressure kPa). The user can create Experiments and collect samples. Each sample is stored as a Run, such that there is a one-to-many relationship from Experiment to Run. In the interest of performance, Run has a to-one relationship with Data entity (which is where the actual raw data is stored); this allows some Run attributes to be loaded without necessarily loading lots of data.

Most of our sensors have multiple measurements, so it would be nice to store all the data that is actually being sampled. But this means that the Run <---> Data relationship needs to become Run <-->> Data (to use Xcode's convention).

I am faced with trying to migrate data from old Run to-one Data model to new Run to-many Data model. Can this be done using Mapping Models? If so, does anyone have any pointers to examples? If not, does anyone have any pointers to examples of how to do that?

Thanks for any pointers or advice.

+1  A: 

That migration should be easy enough that automatic migration will work. Worst case is that it would require a mapping model but I suspect it will "just work" by turning on auto migration.

Marcus S. Zarra
Thanks, Marcus. Alas, between the time that I asked the question and now, the schema has become considerably more complex. However, perusing (i.e. reading thoroughly and carefully), your Core Data book has helped a lot - as has your associated migration code.I fear that I will be subclassing NSEntityMigrationPolicy very, very soon.
westsider
A: 

I ended up needing to subclass NSEntityMigrationPolicy several times. This was necessary because properties were moving from/to different entities with several levels of abstraction being added to support a considerably more general model. Also important was the ordering of entity mappings within the mapping model.

Ultimately, I had to set options for -addPersistentStoreType:configuration:URL:options:error: to:

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];

thus omitting NSInferMappingModelAutomaticallyOption.

westsider