Custom Entity Migration Policy

  • I've specified a custom NSEntityMigrationPolicy subclass to migration
    one of my entities.

    When createRelationshipsForDestinationInstance... is called I retrieve
    the array of relationship expressions I created in the mapping model
    using Xcode.

    I'd like to simple execute all but one of these expressions - I only
    need to customize the relationship mapping for one of the
    relationships, the others are adequately handled using the details
    from the mapping model.

    I've tried a number of different ways to evalute the expression - but
    all seem to give an error :

    Can't get value for 'manager' in bindings {
    }

    in the debugger console when I call :

    [migrationExpression expressionValueWithObject:self context:context];

    I've tried a variety of different objects to evaluate the expression
    against and I can't seem to get past the error message. I've tried
    using the manager argument, the dInstance argument and a dictionary
    (with a "manager" key set to the manager argument). All give the same
    message.

    The documentation is pretty non-existent as to how a subclass of
    NSEnityMigrationPolicy should use some of it's arguments (like the
    NSEntityMapping * argument).

    Has anyone else tried this ? I couldn't find anything about
    subclassing the migration policy when I searched the list archives.

    Thanks

    Andrew 8-)
  • On Dec 2, 2007, at 5:15 PM, Andrew Kimpton wrote:

    > I've specified a custom NSEntityMigrationPolicy subclass to
    > migration one of my entities.
    >
    > When createRelationshipsForDestinationInstance... is called I
    > retrieve the array of relationship expressions I created in the
    > mapping model using Xcode.
    >
    > I'd like to simple execute all but one of these expressions - I only
    > need to customize the relationship mapping for one of the
    > relationships, the others are adequately handled using the details
    > from the mapping model.
    >
    > I've tried a number of different ways to evalute the expression -
    > but all seem to give an error :
    >
    > Can't get value for 'manager' in bindings {
    > }
    >
    > in the debugger console when I call :
    >
    > [migrationExpression expressionValueWithObject:self context:context];
    >

    A little more trial and error and I  came up with :

          NSExpression *migrationExpression = [aPropertyMapping
    valueExpression];
          NSArray *sourceInstancesArray = [manager
    sourceInstancesForEntityMappingNamed:[mapping name]
    destinationInstances:[NSArray arrayWithObject:dInstance]];
          NSManagedObject *source = nil;
          if ([sourceInstancesArray count] > 0)
            source = [sourceInstancesArray objectAtIndex:0];
          NSMutableDictionary *context = [[[NSMutableDictionary alloc]
    init] autorelease];
          [context setValue:manager forKey:@"manager"];
          [context setValue:source forKey:@"source"];
          id expressionResult = [migrationExpression
    expressionValueWithObject:nil context:context];

    Which seems to work fine - but I'm not convinced it's correct ? The
    documentation seems sparse so I might be missing something - anyone
    else care to comment ? In particular is it at all possible to get
    multiple entries in the sourceInstancesArray if only one
    destinationInstance is specified ? Or am I guaranteed to only get a
    one for one result ?

    Andrew 8-)
  • On Dec 2, 2007, at 2:15 PM, Andrew Kimpton wrote:

    > The documentation is pretty non-existent as to how a subclass of
    > NSEnityMigrationPolicy should use some of it's arguments (like the
    > NSEntityMapping * argument).
    >
    Well, there's not much to say beyond, for example, "The mapping object
    in use."  You're free to ask the objects passed as parameters about
    their state etc. in exactly the same way you would any other object
    passed as a parameter in a callback (e.g. <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Pro
    tocols/NSTableDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/
    instm/NSObject/tableView:objectValueForTableColumn:row:
    >).

    As for the general principles of how to subclass, they're given in <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreDataVersionin
    g/Articles/vmMigrationProcess.html
    > ("Custom Entity Migration Policies" > "Three-Stage Migration").

    mmalc
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today