Search code examples
ioscore-datacore-data-migration

iOS: what is the value expression function when migrating coredata relationship?


The function for a relationship is like: FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:","employeesToEmployees",$source.employees")

What is this "Function"? How will it be called? Is there any guide introducing to this?

I've read Apple's

Core Data Model Versioning and Data Migration programming guide

but I still don't get this.


Solution

  • This is a "function expressions with arbitrary method invocations" which seem to be very poorly documented. The only reference that I know of is one paragraph in the NSExpression Class Reference:

    Function Expressions

    On OS X v10.4, NSExpression only supports a predefined set of functions: sum, count, min, max, and average. These predefined functions were accessed in the predicate syntax using custom keywords (for example, MAX(1, 5, 10)).

    On OS X v10.5 and later, function expressions also support arbitrary method invocations. To use this extended functionality, you can now use the syntax FUNCTION(receiver, selectorName, arguments, ...), for example:

    FUNCTION(@"/Developer/Tools/otest", @"lastPathComponent") => @"otest"

    The quoting in that sample code seems be incorrect. But the following code compiles and runs on iOS 5/6:

    NSExpression *expr = [NSExpression expressionWithFormat:@"FUNCTION('/Developer/Tools/otest', 'lastPathComponent')"];
    id result = [expr expressionValueWithObject:nil context:nil];
    NSLog(@"result: %@", result);
    // Output:
    // otest
    

    So in your case, it is a function expression which calls, when evaluated

    [$manager destinationInstancesForEntityMappingNamed:@"employeesToEmployees"
                                        sourceInstances:$source.employees]
    

    where $manager and $source are replaced by the migration manager and the source object, as described in Mapping Model Objects in the "Core Data Model Versioning and Data Migration Programming Guide".