views:

173

answers:

1

I have a fairly simply schema. Essentially, Run <--> Data (where a Run holds a data, e.g., Temperature, sampled from some sort of sensor).

Now, it seems that sensors can have more than one measurement (e.g., Temperature and Humidity). So, a single Run could have multiple data samples.

Hence, Run <-->> Sample and Sample <--> Data. (And for simplicity I am leaving Run <--> Data in place, for now.)

If I create a new mapping model, then things generally work - except that no new Samples are created, no relationships are established between Runs and Samples nor between Samples and Datas.

I am trying to get mapping model to migrate my model but even the slightest change to the generated mapping model results in Cocoa error 134110.

For example, if I take the "Sample" mapping (which has no Source) and set its Source to 'Run' (so that I can set Sample's inverse relationship 'run' appropriately) then the mapping changes its name to "RunToSample". There are two relationships handled in this mapping: data and run. The data property gets set automatically to

FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "DataToData", $source.dataSet)

Following this example, I set the run property to

FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "RunToRun", $source)

Similarly, I set the 'sample' property mapping in RunToRun to

FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "RunToSample", $source)

and the 'sample' property in DataToData to

FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "RunToSample", $source.run)

So, what, I wonder, is going wrong? I have tried various permutations, such as leaving the 'inverse' relationships unspecified. But I continue to get the same error (134110) regardless.

I imagine that this is a lot easier than it seems and that I am missing some fundamental but minor piece. I have also tried subclassing NSEntityMigrationPolicy and overriding -createDestinationInstancesForSourceInstance: but these efforts have met with much the same results.

Thanks in advance for any pointers or (relevant :-) advice.

Edit: For simplicity, I have marked all relationships as optional. Though, ultimately, I may choose otherwise.

A: 

Using the following, I was able to get a better understanding of what was going on (and going wrong):

        NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyDataStore.sqlite"]];

    NSError *error = nil;
    NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
                                                                    URL:storeUrl
                                                                    error:&error];

    if (!sourceMetadata)
        {
        DLog(@"sourceMetadata is nil");
        }
    else
        {
        DLog(@"sourceMetadata is %@", sourceMetadata);
        }

In addition, I needed to carefully order my entity mappings. And, I found that -[NSMigrationManager destinationInstancesForEntityMappingNamed:sourceInstances:] was absolutely necessary for copying 'relationships'. For example,

    NSSet *runs = [source valueForKey:@"allRuns"];
for (Run *srcRun in runs)
    {
    NSArray *temp = [manager destinationInstancesForEntityMappingNamed:@"RunToRun"
                                sourceInstances:[NSArray arrayWithObject:srcRun]];
    if ([temp count])
        {
        Run *dstRun = (Run*)[temp lastObject];
        [dest addAllRunsObject:dstRun];
        }
    }
westsider