2

I have an iOS app whose Core Data model I'm trying to migrate to a new version.

There is one new field (photoFilename) in v2 of the Core Data model, and it's value can be generated for existing entries using a v1 field (dateInSeconds) and appending '.jpg'.

For example

version 1                               | version 2
field          data                     | field          data
========================================|==============================
dateInSeconds (Integer 32)  401760341   | dateInSeconds (Integer 32)  401760341
                                        | photoFilename (String)  401760341.jpg 

I have created a core data mapping model (partially shown below), and the database appears to migrate. Unfortunately, the photoFilename created does not give the correct number in front of the '.jpg'. For example, it might return '275263312.jpg' instead of '401760341.jpg'

enter image description here

Now admittedly, I've never used such mapping before and the Apple documentation here seems very limited.

Researching elsewhere it appears the the 'value expression' is a type of NSExpression and can be replicated for testing purposes.

NSExpression *expr = [NSExpression expressionWithFormat:@"FUNCTION('', 'stringByAppendingFormat:', '%d.jpg', 401760341)"];
id result = [expr expressionValueWithObject:nil context:nil];
NSLog(@"result: %@", result);

However, this evaluates similarly with an incorrect number in front of '.jpg'.

Can someone point out why the evaluated result obtained is not my expected result?

So Over It
  • 3,668
  • 3
  • 35
  • 46

2 Answers2

0

I'm still not sure the exact cause of my problem, but came across the following work around in my mapping model by casting as an NSString

enter image description here

or for those of you following along using the NSExpression

NSExpression *expr = [NSExpression expressionWithFormat:@"FUNCTION('', 'stringByAppendingFormat:', '%@.jpg', CAST(401760341, 'NSString') )"];
id result = [expr expressionValueWithObject:nil context:nil];
NSLog(@"result: %@", result);
So Over It
  • 3,668
  • 3
  • 35
  • 46
0

The solution described at this link worked very well for me.

http://www.cocoabuilder.com/archive/cocoa/197476-custom-entity-migration-policies-atribute-from-nsstring-to-nsdate.html

Briefly, it says you can write a function on any class (a Class method, not instance) in your project (say a utilities class), and invoke that method by using the FUNCTION expression (see the link for specifics on the syntax). This allows you to write fairly complex code to change a particular attribute on entity without writing a whole entity migration policy for the entire entity.

jeffmax
  • 469
  • 4
  • 14