Beware abs function in Core Data
Beware abs function in Core Data
- Subject: Beware abs function in Core Data
- From: Scott Ellsworth <email@hidden>
- Date: Wed, 18 May 2005 13:00:35 -0700
Hi, all.
I just posted bugs
4123401 Use of abs funtion in sqllite store produces 'unsupported
expression type (4)'
and
4123382 Core Data XML store throws selectorNotRecognized when key
path uses abs
The first is a killer, the second merely annoying.
My World object has a 1-1 with a Location object. Inside the
Location are normalizedRow and normalizedColumn int16 attributes.
I want those worlds that are close. The formula is the one I
mentioned yesterday:
int deltaNormCol = abs(originNormCol-destinationNormCol);
int deltaNormRow = abs(originNormRow-destinationNormRow);
if (deltaNormCol>deltaNormRow){
return deltaNormCol/2;
} else {
return (deltaNormCol + deltaNormRow)/4;
}
(Those who saw my post of yesterday where I was talking about this -
I worked out the data costs of storing a pair of int16s in each
World, and decided that it was just not that big a deal.)
To implement this, I created the predicate against the Location entity:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((abs(%@
- normalizedColumn) > abs(%@ - normalizedRow) AND (abs(%@ -
normalizedColumn) / 2 < %@)) OR ((abs(%@ - normalizedColumn) <= abs(%
@ - normalizedRow)) AND (abs(%@ - normalizedColumn) + abs(%@ -
normalizedRow) <= %@)))",[world
valueForKeyPath:@"location.normalizedColumn"], [world
valueForKeyPath:@"location.normalizedRow"], [world
valueForKeyPath:@"location.normalizedColumn"], [NSNumber
numberWithInt:3],[world
valueForKeyPath:@"location.normalizedColumn"], [world
valueForKeyPath:@"location.normalizedRow"], [world
valueForKeyPath:@"location.normalizedColumn"], [world
valueForKeyPath:@"location.normalizedRow"], [NSNumber numberWithInt:3]];
When you run this in sqlite, you get
2005-05-18 12:21:35.354 Astrogator[16849] unsupported expression type
(4): abs:(-88 - normalizedColumn)
but you get correct answers with an XML store.
One tedious problem - xml stores seems to not handle predicates with
keypaths inside functions, so abs(%@ - location.normalizedColumn)
fails with
2005-05-18 12:28:18.672 Astrogator[17213] *** -[NSXMLDocumentMapNode
objCType]: selector not recognized [self = 0x1d18ab50]
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden